I did a quick search on the forums and there was another problem similar to the one i'm trying to solve.
I'm supposed to use the first word of a sentence as a search parameter and check the rest of the string for occurences of the word. If the word appears again, increment a counter.
The problem I'm having is that when I use the function strstr() in a while loop, the console hangs :rolleyes: .
Correct me if i'm wrong, but the strstr() function returns a NULL if the substring isn't found in the main string. So, if my sentence was "the cat sat on the mat" the loop would terminate after the second "the."
> The problem I'm having is that when I use the function strstr() in a while loop, the console hangs
Well you don't advance the pointer, so it always begins the search from the same point, always finds the same match, and so on on on on .....
> gets(array);
NEVER use gets() to read input, there is no way to make it safe.
Always use fgets().
> char string[SIZE];
This should really be a local variable in some function.
Whether it's in main, and passed as a parameter to your function, or as a local within your function is up to you.
> while (*ptr != '\0')
This loop doesn't seem to append the \0 to the string you create.
to degamer 106:
hey there, looking at your source code was a big help to me but i think it might be better to step 'strptr' through the entire length of 'word', like this:
strptr=strptr+strlen(word);
i think this could be faster, but please correct me if i have made a wrong judgement, waiting to here from you,
cheers!
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.