help with c code

Bob Beaulieu bobb at netcom.UUCP
Tue Jan 22 16:06:21 AEST 1991


I'm new to new to C and am trying to write a program to cut a name field
into a first,middle and last fields. Problem: some names don't have middle
names or initials, and some others have more (i.e. SR.,JR.,III,...). I
feel I'm fairly close,but probably inefficient with my code. Any suggestions
would be greatly appreciated.

example of "ttt.txt":

~~~~~~~~~~~~~~~~~~~~
"John J. Jackson"
"J. Johnson"
"Betty Smith"
"Charles Robinson Sr."
"Cindy Ann West"

~~~~~~~~~~~~~~~~~~~~

Thanks,
BobB


#include <stdio.h>

main()
{
FILE *fopen(),*fp;
int c=0,a=0,b=0,count=1;
char buf[100],firstname[100],midname[100],lastname[100];
char *fgets();

fp = fopen("test","r");

while (fgets(buf,100,fp) !=NULL) 
  {
    for(c=0;c<=strlen(buf);c++)
       {
          switch(buf[c])
              {
                 case  ' ': count++; break;
                 default  : switch(count)
                               {
                                case 1: firstname[c] = buf[c]; break;
                                case 2: midname[a++] = buf[c]; break;
                                case 3: lastname[b++] = buf[c];break;
                               default: break;
                               }
              }

        }
     if(lastname[1] == ' ')
       {
         for(c=0;c<=strlen(midname);c++)  lastname[c] = midname[c];
         for(c=0;c<=strlen(lastname);c++) midname[c] = ' ';
       }
     printf("%s|%s|%s",firstname,midname,lastname);
     count=1;

     for(c=0;c<strlen(buf);c++)
       {
     firstname[c] = ' ';
     midname[c] = ' ';
     lastname[c] = ' ';
       }
     a=0;c=0,b=0;
  }
fclose(fp);
exit(0);
}
-- 
{\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\}
{                               Bob Beaulieu                            } 
{                               San Jose, CA.                           }
{                              (408) 723-0556                           } 
{                          uunet!apple!netcom!bobb                      }
{\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\}



More information about the Comp.lang.c mailing list