943,521 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9670
  • C RSS
Mar 25th, 2006
0

using strstr to find words

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Mar 25th, 2006
1

Re: using strstr to find words

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Mar 27th, 2006
0

Re: using strstr to find words

@ Salem
congrats for such a good and complete reply.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Mar 27th, 2006
0

Re: using strstr to find words

@dubeyprat :eek:

You can add to his/her rep if you thought it was good.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 27th, 2006
0

Re: using strstr to find words

: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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Dec 13th, 2007
0

Re: using strstr to find words

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aikem-un is offline Offline
1 posts
since Dec 2007

This thread is more than three months old

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.
Message:
Previous Thread in C Forum Timeline: Binary Tree
Next Thread in C Forum Timeline: program that will input a number and display it in words





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC