cheers hammer, brad.
cheers hammer, brad.
after putting this code into my programme:
printf("\n\nOk, please enter a word to look for in this sentence.\n");
scanf("%s", &word);
char *delims = { " .," };
char *ptr;
int count = 0;
ptr = strtok(sentence2, delims);
while (ptr != NULL)
{
if (strcmp(sentence2, word) ==0)
count++;
ptr = strtok(NULL, delims);
}
printf("Found word count %d times\n", count);
}
it seems only to count the word entered if its at the start of the sentence, and then counts every word after it. otherwise if it isnt the starting word it just counts a total of 0. how come?
thanks, brad.
Last edited by brad123; 04-29-2002 at 12:05 PM.
You're comparing the wrong values in strcmp. Use strcmp(ptr, word).Originally posted by brad123
Code:while (ptr != NULL) { if (strcmp(sentence2, word) ==0) count++;
The idea of strtok in this implementation is that you get a pointer (ptr) to each word in the sentence, and process it one by one.
Also, you best read about code tags if you're gonna post code here:
http://www.cprogramming.com/cboard/m...&action=bbcode
Last edited by Hammer; 04-29-2002 at 12:51 PM.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]