using strstr to find words

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

using strstr to find words

 
0
  #1
Mar 25th, 2006
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."

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. #define SIZE 100
  7. #define FLAG 1
  8.  
  9. void search_string(char *);
  10.  
  11. char string[SIZE];
  12. int main(void)
  13. {
  14. char array[SIZE];
  15.  
  16. puts("Enter a string:");
  17. gets(array);
  18. search_string(array);
  19.  
  20. return 0;
  21. }
  22.  
  23. void search_string(char * array)
  24. {
  25. char *ptr, *strptr;
  26. int i = 0, j = 1;
  27. ptr = array;
  28.  
  29. while (*ptr != '\0')
  30. {
  31. if (isspace(*ptr))
  32. break;
  33. string[i] = array[i]; //put each char from array into string until a space is found.
  34. ptr++;
  35. i++;
  36. }
  37.  
  38. strptr = strstr(array, string);
  39.  
  40. /*
  41. while (FLAG == 1)
  42. {
  43.  
  44. if (strptr != 0)
  45. j++;
  46. else
  47. FLAG == 0;
  48. }
  49. */
  50. printf("The first word is: %s\n", string);
  51. printf("The sentence is: %s\n", array);
  52. printf("The first word occured %d times in the sentence\n", j);
  53.  
  54. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: using strstr to find words

 
1
  #2
Mar 25th, 2006
> 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 .....

  1. while ( (p=strstr(p,"thing")) != NULL ) {
  2. // do something
  3.  
  4. // start search from just past the previous hit
  5. p++;
  6. }

> 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: using strstr to find words

 
0
  #3
Mar 27th, 2006
@ Salem
congrats for such a good and complete reply.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: using strstr to find words

 
0
  #4
Mar 27th, 2006
@dubeyprat :eek:

You can add to his/her rep if you thought it was good.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: using strstr to find words

 
0
  #5
Mar 27th, 2006
:cry: :cry: :cry: :cry: :cry:

Ok i finally solved it. Thanks for the help

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define SIZE 100
  6.  
  7. void search_string(char *);
  8.  
  9. int main(void)
  10. {
  11. char array[SIZE];
  12.  
  13. puts("Enter a string:");
  14. gets(array);
  15. search_string(array);
  16.  
  17. return 0;
  18. }
  19.  
  20. void search_string(char *array)
  21. {
  22. char *ptr, *strptr, word[SIZE] = {""};
  23. int i = 0, j = 0;
  24.  
  25. ptr = array;
  26.  
  27. while (*ptr != '\0')
  28. {
  29. if (isspace(*ptr))
  30. break;
  31. word[i] = array[i];
  32. ptr++;
  33. i++;
  34. }
  35. strptr = array;
  36.  
  37. while ((strptr = strstr(strptr, word)) != NULL)
  38. {
  39. strptr++;
  40. j++;
  41. }
  42.  
  43. puts(array);
  44. printf("%s occured %d times in the string\n", word, j);
  45. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1
Reputation: aikem-un is an unknown quantity at this point 
Solved Threads: 0
aikem-un aikem-un is offline Offline
Newbie Poster

Re: using strstr to find words

 
0
  #6
Dec 13th, 2007
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!
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC