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: