Hello folks,

First timer here so bear with me. I'm trying to tokenize an array of strings.

This program reads an array of strings, each string has a First and Last Name and some have Middle Names. The array of strings is then bubble sorted(not the most efficient but required)
The problem im having is this:

I'm trying to
strcpy(MiddleName[a],tokenPtr) if there is a Middle Name
otherwise Middle name returns a Null

strcpy(LastName[g],tokenPtr)

as you can see, I am tokenizing each string and successfully managed to get the FirstName array. I am having problems with the rest using strtok...primarily crashing the programing.
I'm not sure how to code the following(assuming it is correct)

if there is nothing else to tokenize(two name string), dont tokenize it
tokenize the rest of the elements(3 name string)

I think im confusing myself

**Note: The spaces between names are purposely there

/*********************************************************
 *This program takes string literals                     *
 *and bubble sorts them in ascending and descending order*
 *********************************************************/

#include<iostream.h>

#include<string.h>


int main()
{
    char Names[][100] = { "Forest Gump", "George Herbert Wash", "Mark Yan",   
		"Sonny  Harris", "Mel  Gat", "Samantha    Gert",
		"George   Tim Harold", "Eric Arthur Young" };      //name list to be sort

	char NamesCopy[8][100];              //will be used to hold a copy of the namelist

	char FirstName[8][100];               //array for the first name

	char MiddleName[8][20];               //array for the middle name

	char LastName[8][20];                //array for the last name

	char *tokenPtr;                      //declaring token pointer

	

    cout << "Original Names"<<endl<<"--------------"<<endl;      //output line

	for(int i=0;i<8;i++)

	{
		
		cout<<Names[i]<<endl;            //displays names list

		strcpy(NamesCopy[i],Names[i]);   //made a copy of the original name list

	    
	}

		cout<<endl<<endl;

	for(int j=0;j<8;j++)

	{

		

	
			tokenPtr=strtok(NamesCopy[j]," ");      //tokenizing names

			strcpy(FirstName[j],NamesCopy[j]);      //copying the first tokenized name to first name array

			//	cout<<FirstName[j]<<endl;

			tokenPtr=strtok(NULL, " ");
			
		//	if (tokenPtr='\0')
	

		//	else
				

	
	
			cout<<tokenPtr<<endl;
		
    
	}
		

	for(int k=0;k<8;k++)       

	{
			
		strlen(FirstName[k]);     //finds the length of the first name


	}
	


	for(int z=0;z<8;z++)                     //begin loop control for comparing names

	{
		if(FirstName[z]>LastName[z])
		
		{
			Names[z]>Names[z+1];

	}else if(LastName[z]==LastName[z+1]&&FirstName[z]>FirstName[z+1])
		
		{


		Names[z]>Names[z+1];

	}else if(LastName[z]==LastName[z+1]&&FirstName[z]==FirstName[z+1]&&MiddleName[z]>MiddleName[z+1])

	{
		if(MiddleName[z]==" ")

		{
			MiddleName==NULL;

		}

	}

	}//end loop


	return 0;

}

Thanks folks

No more coffee for me... :eek:

Since the program can't know in advance whether a given name in Names will have 2 or three tokens, I would use an array of char * store each token as it was developed, and an index to keep track of how many tokens found in each string. After each string has been tokenized the string, then look at the value of the index. If the value of the index indicates there were two tokens found, then there is no middle name, so enter null instead in the array of middle names.

declare array of tokens;
declare index and set it to 0

see if there is a token
while there are more tokens to find
 {
     add token to token array at appropriate index
     increment index
}
add firtst element in array of tokens to array of first names
if index equals 1 //meaning only two names found
  add null to array of middle names.
  add second element of array of tokens to array of last names
else
  add second element in array of tokens to array of middle names
  add third element in array of tokens to array of last names

Also note that you can't use the == to compare null terminated strings, you must use strcmp(), or a function related to strcmp() like strncmp();

Since the program can't know in advance whether a given name in Names will have 2 or three tokens, I would use an array of char * store each token as it was developed, and an index to keep track of how many tokens found in each string. After each string has been tokenized the string, then look at the value of the index. If the value of the index indicates there were two tokens found, then there is no middle name, so enter null instead in the array of middle names.

declare array of tokens;
declare index and set it to 0

see if there is a token
while there are more tokens to find
 {
     add token to token array at appropriate index
     increment index
}
add firtst element in array of tokens to array of first names
if index equals 1 //meaning only two names found
  add null to array of middle names.
  add second element of array of tokens to array of last names
else
  add second element in array of tokens to array of middle names
  add third element in array of tokens to array of last names

Also note that you can't use the == to compare null terminated strings, you must use strcmp(), or a function related to strcmp() like strncmp();

Thanks for the help Lerner. I'm still having trouble copying the token array elements to their respective arrays...(i.e. strcpy(FirstName[j],tokenStore[0])...
here is what I have...

/*********************************************************
 *This program takes string literals                     *
 *and bubble sorts them in ascending and descending order*
 *********************************************************/
#include<iostream.h>
#include<string.h>
#include<stdlib.h>

int main()
{
 char Names[][100] = { "Edward Chen", "George Robert Harris", "Jan Jop",   
  "Belinda  Harris", "Dennis  Miller", "Sid    Gore",
  "George   Tom Harris", "Eddy Arthur Cart" };      //name list to be sort
 char NamesCopy[8][100];              //will be used to hold a copy of the namelist
 char FirstName[8];               //array for the first name
 char MiddleName[8][20];               //array for the middle name
 char LastName[8][20];                //array for the last name
 char *tokenPtr;                      //declaring token pointer
 char *tokenStore[3];                    //ptr to an array of tokens

 
    cout << "Original Names"<<endl<<"--------------"<<endl;      //output line
 for(int i=0;i<8;i++)
 {
  
  cout<<Names[i]<<endl;            //displays names list
  strcpy(NamesCopy[i],Names[i]);   //made a copy of the original name list
     
 }
  cout<<endl<<endl;
 
  //if(tokenPtr!=NULL)                             //if there is a token

	  {	
  
	 for(int j=0;j<8;j++)             //loop for element index control
 
 	 {
  

		  int z = 0 ;                               //index declared
		  
		  tokenPtr=strtok(NamesCopy[j]," ");        //tokenizing names
        
		
                                        
		  while(tokenPtr!=NULL)                     //while there are more tokens to find
  
		  { 
			 int len = strlen(tokenPtr);            // length of word just read
        
			 tokenStore[z] = new char[len+1];       // allocate space for word	
        	 		 
			 strcpy(tokenStore[z],tokenPtr);        //copying the  tokenized name to token  array
			 
   			 tokenPtr=strtok(NULL, " ");            //get the next token

			 cout<<j<<" "<<z<<" "<<len<<" " << tokenStore[z]<<endl;

			  z++;                                 //increment index
	
		  }//end while loop
	
        	if(z==1)
			 {

				strcpy(FirstName,tokenStore[0]);

				 cout<<"Crappo"<<endl;

				  MiddleName[j]==NULL;
 /*
				 strcpy(LastName[j],tokenStore[1]);

			 }	 else{

					 strcpy(MiddleName[j],tokenStore[1]);

				     strcpy(LastName[j],tokenStore[2]);
*/
			 }	 
	//	delete [] tokenStore[z];                   //free up array
	//	tokenStore[z]=NULL;
//delete [] wordList[i];       // free space
      //  wordList[i] = NULL;          // remove pointer
		// cout<<MiddleName[j]<<endl;

		// cout<<LastName[j]<<endl;

	
		  


	  }	  
			
   }  


 for(int k=0;k<8;k++)       
 {
   
  cout<<FirstName[k]<<endl;     //finds the length of the first name

 }
 
/*
 for(int z=0;z<8;z++)                     //begin loop control for comparing names
 {
  if(FirstName[z]>LastName[z])
  
  {
   Names[z]>Names[z+1];
 }else if(LastName[z]==LastName[z+1]&&FirstName[z]>FirstName[z+1])
  
  {

  Names[z]>Names[z+1];
 }else if(LastName[z]==LastName[z+1]&&FirstName[z]==FirstName[z+1]&&MiddleName[z]>MiddleName[z+1])
 {
  if(MiddleName[z]==" ")
  {
   MiddleName==NULL;
  }
 }
 }//end loop
 
 */

 
  
 
 
 return 0;
}

if you run the program you can see that the FirstNames array has not taken any values...This is about 8 hrs worth of work and still nothing...everytime i try, I crash the program...

This article has been dead for over six months. Start a new discussion instead.