Is there a way to tokenize an array of strings

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2005
Posts: 2
Reputation: Halcyon is an unknown quantity at this point 
Solved Threads: 0
Halcyon Halcyon is offline Offline
Newbie Poster

Is there a way to tokenize an array of strings

 
0
  #1
Dec 3rd, 2005
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

  1.  
  2.  
  3. /*********************************************************
  4.  *This program takes string literals *
  5.  *and bubble sorts them in ascending and descending order*
  6.  *********************************************************/
  7.  
  8. #include<iostream.h>
  9.  
  10. #include<string.h>
  11.  
  12.  
  13. int main()
  14. {
  15. char Names[][100] = { "Forest Gump", "George Herbert Wash", "Mark Yan",
  16. "Sonny Harris", "Mel Gat", "Samantha Gert",
  17. "George Tim Harold", "Eric Arthur Young" }; //name list to be sort
  18.  
  19. char NamesCopy[8][100]; //will be used to hold a copy of the namelist
  20.  
  21. char FirstName[8][100]; //array for the first name
  22.  
  23. char MiddleName[8][20]; //array for the middle name
  24.  
  25. char LastName[8][20]; //array for the last name
  26.  
  27. char *tokenPtr; //declaring token pointer
  28.  
  29.  
  30.  
  31. cout << "Original Names"<<endl<<"--------------"<<endl; //output line
  32.  
  33. for(int i=0;i<8;i++)
  34.  
  35. {
  36.  
  37. cout<<Names[i]<<endl; //displays names list
  38.  
  39. strcpy(NamesCopy[i],Names[i]); //made a copy of the original name list
  40.  
  41.  
  42. }
  43.  
  44. cout<<endl<<endl;
  45.  
  46. for(int j=0;j<8;j++)
  47.  
  48. {
  49.  
  50.  
  51.  
  52.  
  53. tokenPtr=strtok(NamesCopy[j]," "); //tokenizing names
  54.  
  55. strcpy(FirstName[j],NamesCopy[j]); //copying the first tokenized name to first name array
  56.  
  57. // cout<<FirstName[j]<<endl;
  58.  
  59. tokenPtr=strtok(NULL, " ");
  60.  
  61. // if (tokenPtr='\0')
  62.  
  63.  
  64. // else
  65.  
  66.  
  67.  
  68.  
  69. cout<<tokenPtr<<endl;
  70.  
  71.  
  72. }
  73.  
  74.  
  75. for(int k=0;k<8;k++)
  76.  
  77. {
  78.  
  79. strlen(FirstName[k]); //finds the length of the first name
  80.  
  81.  
  82. }
  83.  
  84.  
  85.  
  86. for(int z=0;z<8;z++) //begin loop control for comparing names
  87.  
  88. {
  89. if(FirstName[z]>LastName[z])
  90.  
  91. {
  92. Names[z]>Names[z+1];
  93.  
  94. }else if(LastName[z]==LastName[z+1]&&FirstName[z]>FirstName[z+1])
  95.  
  96. {
  97.  
  98.  
  99. Names[z]>Names[z+1];
  100.  
  101. }else if(LastName[z]==LastName[z+1]&&FirstName[z]==FirstName[z+1]&&MiddleName[z]>MiddleName[z+1])
  102.  
  103. {
  104. if(MiddleName[z]==" ")
  105.  
  106. {
  107. MiddleName==NULL;
  108.  
  109. }
  110.  
  111. }
  112.  
  113. }//end loop
  114.  
  115.  
  116. return 0;
  117.  
  118. }

Thanks folks

No more coffee for me... :eek:
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 266
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Is there a way to tokenize an array of strings

 
0
  #2
Dec 3rd, 2005
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.
  1. declare array of tokens;
  2. declare index and set it to 0
  3.  
  4. see if there is a token
  5. while there are more tokens to find
  6. {
  7. add token to token array at appropriate index
  8. increment index
  9. }
  10. add firtst element in array of tokens to array of first names
  11. if index equals 1 //meaning only two names found
  12. add null to array of middle names.
  13. add second element of array of tokens to array of last names
  14. else
  15. add second element in array of tokens to array of middle names
  16. 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();
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 2
Reputation: Halcyon is an unknown quantity at this point 
Solved Threads: 0
Halcyon Halcyon is offline Offline
Newbie Poster

Re: Is there a way to tokenize an array of strings

 
0
  #3
Dec 6th, 2005
Originally Posted by Lerner
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.
  1. declare array of tokens;
  2. declare index and set it to 0
  3.  
  4. see if there is a token
  5. while there are more tokens to find
  6. {
  7. add token to token array at appropriate index
  8. increment index
  9. }
  10. add firtst element in array of tokens to array of first names
  11. if index equals 1 //meaning only two names found
  12. add null to array of middle names.
  13. add second element of array of tokens to array of last names
  14. else
  15. add second element in array of tokens to array of middle names
  16. 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...

  1.  
  2. /*********************************************************
  3.  *This program takes string literals *
  4.  *and bubble sorts them in ascending and descending order*
  5.  *********************************************************/
  6. #include<iostream.h>
  7. #include<string.h>
  8. #include<stdlib.h>
  9.  
  10. int main()
  11. {
  12. char Names[][100] = { "Edward Chen", "George Robert Harris", "Jan Jop",
  13. "Belinda Harris", "Dennis Miller", "Sid Gore",
  14. "George Tom Harris", "Eddy Arthur Cart" }; //name list to be sort
  15. char NamesCopy[8][100]; //will be used to hold a copy of the namelist
  16. char FirstName[8]; //array for the first name
  17. char MiddleName[8][20]; //array for the middle name
  18. char LastName[8][20]; //array for the last name
  19. char *tokenPtr; //declaring token pointer
  20. char *tokenStore[3]; //ptr to an array of tokens
  21.  
  22.  
  23. cout << "Original Names"<<endl<<"--------------"<<endl; //output line
  24. for(int i=0;i<8;i++)
  25. {
  26.  
  27. cout<<Names[i]<<endl; //displays names list
  28. strcpy(NamesCopy[i],Names[i]); //made a copy of the original name list
  29.  
  30. }
  31. cout<<endl<<endl;
  32.  
  33. //if(tokenPtr!=NULL) //if there is a token
  34.  
  35. {
  36.  
  37. for(int j=0;j<8;j++) //loop for element index control
  38.  
  39. {
  40.  
  41.  
  42. int z = 0 ; //index declared
  43.  
  44. tokenPtr=strtok(NamesCopy[j]," "); //tokenizing names
  45.  
  46.  
  47.  
  48. while(tokenPtr!=NULL) //while there are more tokens to find
  49.  
  50. {
  51. int len = strlen(tokenPtr); // length of word just read
  52.  
  53. tokenStore[z] = new char[len+1]; // allocate space for word
  54.  
  55. strcpy(tokenStore[z],tokenPtr); //copying the tokenized name to token array
  56.  
  57. tokenPtr=strtok(NULL, " "); //get the next token
  58.  
  59. cout<<j<<" "<<z<<" "<<len<<" " << tokenStore[z]<<endl;
  60.  
  61. z++; //increment index
  62.  
  63. }//end while loop
  64.  
  65. if(z==1)
  66. {
  67.  
  68. strcpy(FirstName,tokenStore[0]);
  69.  
  70. cout<<"Crappo"<<endl;
  71.  
  72. MiddleName[j]==NULL;
  73. /*
  74. strcpy(LastName[j],tokenStore[1]);
  75.  
  76. } else{
  77.  
  78. strcpy(MiddleName[j],tokenStore[1]);
  79.  
  80. strcpy(LastName[j],tokenStore[2]);
  81. */
  82. }
  83. // delete [] tokenStore[z]; //free up array
  84. // tokenStore[z]=NULL;
  85. //delete [] wordList[i]; // free space
  86. // wordList[i] = NULL; // remove pointer
  87. // cout<<MiddleName[j]<<endl;
  88.  
  89. // cout<<LastName[j]<<endl;
  90.  
  91.  
  92.  
  93.  
  94.  
  95. }
  96.  
  97. }
  98.  
  99.  
  100. for(int k=0;k<8;k++)
  101. {
  102.  
  103. cout<<FirstName[k]<<endl; //finds the length of the first name
  104.  
  105. }
  106.  
  107. /*
  108.  for(int z=0;z<8;z++) //begin loop control for comparing names
  109.  {
  110.   if(FirstName[z]>LastName[z])
  111.  
  112.   {
  113.   Names[z]>Names[z+1];
  114.  }else if(LastName[z]==LastName[z+1]&&FirstName[z]>FirstName[z+1])
  115.  
  116.   {
  117.  
  118.   Names[z]>Names[z+1];
  119.  }else if(LastName[z]==LastName[z+1]&&FirstName[z]==FirstName[z+1]&&MiddleName[z]>MiddleName[z+1])
  120.  {
  121.   if(MiddleName[z]==" ")
  122.   {
  123.   MiddleName==NULL;
  124.   }
  125.  }
  126.  }//end loop
  127.  
  128.  */
  129.  
  130.  
  131.  
  132.  
  133.  
  134. return 0;
  135. }

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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC