[comp.lang.c] help with c code

bobb@netcom.UUCP (Bob Beaulieu) (01/22/91)

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                      }
{\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\}

laned@athena.ecs.csus.edu (Douglas A. Lane) (01/25/91)

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 <stdio.h>
#include <string.h>

#ifdef TURBOC
#include <stdlib.h> 
#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;
}

torek@elf.ee.lbl.gov (Chris Torek) (02/14/91)

In article <21584@netcom.UUCP> bobb@netcom.UUCP (Bob Beaulieu) writes:
>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,...).

These are not the only problems.  For instance, consider the name
`Maria Lupita Josefa Carantinez y Pallano de la Torre-Wallace'.
Find the last name. :-)

The usual solution to the mess is to have the user mark the beginning of
the last name.  Modern name databases are now getting better at handling
things other than `First Name, Middle Initial, Last Name' sequences,
which is a good thing for those who use something else.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427)
Berkeley, CA		Domain:	torek@ee.lbl.gov