Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!apple!netcom!bobb From: bobb@netcom.UUCP (Bob Beaulieu) Newsgroups: comp.lang.c Subject: help with c code Keywords: trying to separate whole name to 1st,middle & last Message-ID: <21584@netcom.UUCP> Date: 22 Jan 91 05:06:21 GMT Organization: Netcom- The Bay Area's Public Access Unix System {408 241-9760 guest} Lines: 76 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 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