using strtok

plz help me for this. i was trying to find similar words from two sentences string1 and string2.but i having problem with using strtok and strcpy together.plz some one help me to do this.my program i have copy below :


char string1[81];
cout<< "please enter 1st line:";
for(int i=0; i<n1; i++)
{
cin>> string1[i];
}

char string2[81];
cout<< "please enter 2nd line:";
for(int j=0; j<n2; j++)
{
cin>> string2[j];
}

char seps[]= " ";
char words1[15][20];
char words2[15][20];
int i;
int j;
int count01;
int count02;

count01=0;
strcpy(words1[0],strtok (string1, seps)); //strcpy use karanna
while( words1[count01] !=NULL)
{
count01++;
strcpy(words1[count01],strtok(NULL, seps));
}


count02=0;
strcpy(words2[0],strtok (string2, seps));
while( words2[count02] !=NULL)
{
count01++;
strcpy(words2[count02],strtok(NULL, seps));
}
strtok is horrible because it changes the buffer it parses. Your use of strtok is incorrect.

http://www.cplusplus.com/reference/string/string/c_str/
1. Remember to use code tags to properly format your code.
2. You should use the std::getline() function instead of a for() loop to get input from the user.
3. If your compiler knows about it, use strtok_s() instead.
4. Your second while loop uses the wrong counter. Copy/paste much? :-)
5. You need to protect your loops in the case more than 15 words are found and in the case the token (the word) is longer than 19 characters. Otherwise you'll corrupt the stack.
6. I believe you are missing the last token if the sentence doesn't end with a space, or in general, with one of the defined delimeters (you have defined the space only so far). You should also define punctuation symbols as delimeters.
Topic archived. No new replies allowed.