Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!sdd.hp.com!wuarchive!csus.edu!ucdavis!csusac!usenet From: laned@athena.ecs.csus.edu (Douglas A. Lane) Newsgroups: comp.lang.c Subject: Re: help with c code Keywords: trying to separate whole name to 1st,middle & last Message-ID: <1991Jan25.053555.8493@csusac.csus.edu> Date: 25 Jan 91 05:35:55 GMT References: <21584@netcom.UUCP> Sender: usenet@csusac.csus.edu (News account (alt)) Followup-To: laned@athena.ecs.csus.edu Organization: California State University, Sacramento Lines: 270 B, This code performs what you requested.. hope it helps in your quest to learn more about C. This compiles under BSD cc, Turbo C++ 1.0, ANSI C.. Doug daLane (laned@athena.ecs.csus.edu) ---------------------------------------------------------------------------- The opinions expressed are mine alone and do not reflect any organization unless noted. ============================================================================ "The best religion, is tolerance" "Take a stand, but not on my feet please." -me ---------------------------------------------------------------------------- "Men are disturbed not by things that happen but by their opinion of things that happen." Epictetus ---------------------------------------------------------------------------- SUPPORT OUR TROOPS !! When they come home - welcome them, they deserve it. ------------------------------------------------------------------------------ Hussein did not come to negociate, neither did Bush - anyone who thinks they would is a fool. ------------------------------------------------------------------------------ /* #define ANSIC */ /* #define TURBOC */ /* * Purpose * * This program will read the input file of names and transpose * then into the form: * last, first, middle * or * last, first * * The input is in the form of: * [prefix] first [middle] last [suffix] * * The program will strip of a select number of suffixes and prefixes. * The program will ignore all blanks, but assumes the only delimiters * between the names are blanks (no commas allowed) * * --------------------------------------------------------------------------- * Sample file ( "test" ) * Mr. John J. Jackson * J. Johnson * Betty Smith * Charles Robinson Sr. * Charles Robinson Jr. * Cindy Ann West * Miss Cindy Ann West * Ms. Cindy West * Charle Emerson Winchester III * --------------------------------------------------------------------------- * * Sample Output * --------------------------------------------------------------------------- * Jackson, John, J. * Johnson, J., Johnson * Smith, Betty, Smith * Robinson, Charles, Robinson * Robinson, Charles, Robinson * West, Cindy, Ann * West, Cindy, Ann * West, Cindy, West * Winchester, Charle, Emerson * --------------------------------------------------------------------------- * * - Author - * Douglas Lane, Sacramento Calfornia * programmer 6 years, C programmer 3 years. * */ #include #include #ifdef TURBOC #include #endif #define MAXBUFNAME 100 #define TESTFILE "test" /* ----------------------------------------------------------------------- * * getname - get a single name * * returns index of next word in istr. * ----------------------------------------------------------------------- */ #ifdef ANSIC int getname(char *inname, char *thename, int startidx) #else int getname(inname, thename, startidx) char *inname; char *thename; int startidx; #endif { int idx; int num; idx = 0; for (num=startidx; inname[num] != ' ' && inname[num] != '\0'; num++) { thename[idx] = inname[num]; idx ++; } thename[idx] = '\0'; while (inname[num] == ' ') num++; return num; } /* ----------------------------------------------------------------------- * * not first name * * not a valid first name * ----------------------------------------------------------------------- */ #ifdef ANSIC int notfirstname(char *thename) #else int notfirstname(thename) char *thename; #endif { return( (strcmp(thename, "Mr.")==0) || (strcmp(thename, "Mrs.")==0) || (strcmp(thename, "Miss")==0) || (strcmp(thename, "Ms.")==0) ); } /* ----------------------------------------------------------------------- * * not last name * * not a valid last name * ----------------------------------------------------------------------- */ #ifdef ANSIC int notlastname(char *thename) #else int notlastname(thename) char *thename; #endif { return( (strcmp(thename, "Sr.")==0) || (strcmp(thename, "Jr.")==0) || (strcmp(thename, "III")==0) ); } /* ----------------------------------------------------------------------- * * splitname * * split name into first, middle, last * ----------------------------------------------------------------------- */ #ifdef ANSIC int splitname(char *fullname, char *first, char *middle, char *last) #else int splitname(fullname, first, middle, last) char *fullname; char *first; char *middle; char *last; #endif { int numnames; int idx; numnames = 0; while (fullname[0]==' ') /* this left justifies the string */ fullname++; /* -- first name -- */ idx = getname(fullname, first, 0); if (notfirstname(first)) idx = getname(fullname, first, idx); if (first[0]!='\0') numnames++; /* -- middle name -- */ idx = getname(fullname, middle, idx); if (middle[0]!='\0') numnames++; /* -- last name -- */ idx = getname(fullname, last, idx); if (last[0]!='\0') numnames++; /* -- remove middle name -- */ if (notlastname(last) || (last[0]=='\0')) { strcpy(last,middle); middle = '\0'; } return numnames; } main() { char buf[MAXBUFNAME]; char first[MAXBUFNAME]; char mid[MAXBUFNAME]; char last[MAXBUFNAME]; FILE *fp; int namecount; fp = fopen(TESTFILE,"r"); if (fp!=NULL) { while (fgets(buf,MAXBUFNAME,fp) !=NULL) { buf[strlen(buf)-1] = '\0'; namecount = splitname(buf, first, mid, last); if (namecount == 2) printf(" %20s, %s \n",last,first); else printf(" %20s, %s, %s \n",last,first,mid); } fclose(fp); exit(0); } else fprintf(stderr," could not open %s \n",TESTFILE); return 1; }