469,621 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

help with counting substings in strings???

Im a newby to programing 2 months exp.. I am trying to write code in C to take a input string from the user and then the input substring. I wish to compare the substring to the string and count the number of times it apears and print this to the user. After many hours of frustration i have gotten this far any help or derection would be aprieciated
Expand|Select|Wrap|Line Numbers
  1. # include<stdio.h>  
  2. # include<math.h>
  3. # include<stdlib.h>
  4. # include<string.h>
  5.  
  6. int main(void)
  7.  
  8. {
  9. char s1[200], s2[200];
  10. char count;
  11. int count2;
  12. int run;
  13. int i;
  14. int j;
  15. int strstrcnt(const char *s1,const char *s2);
  16. char s[200];
  17.  
  18. printf("\nThis is a program to find certian occurances of words in a user inputed sentence\n");
  19. printf("\nIt will display the number of times the word is found.\n");
  20.  
  21. printf("\nTo run this program and input the word to find and the sentence to search hit 1,\n\n Hit 2 to exit.\n");
  22.  
  23. scanf("%d",&run);
  24.  
  25. fflush(stdin);
  26.  
  27. while (run==1)
  28.  
  29. {printf("\nPlease input the sentence you wish to hae searched.\n");
  30.  
  31.  gets(s1);
  32.  
  33.  printf("\nPlease enter the word or term you wish to have found in the sentence.\n");
  34.  
  35.  gets(s2);
  36.  
  37.  strstrcnt(s1,s2);
  38.  
  39.  printf("%d",s1[0]);
  40.  
  41.  
  42. }
  43.  
  44. return(0);}
  45.  
  46. int strstrcnt(const char *s1,const char *s2)
  47. {
  48. char * strstr(const char *s1, const char *s2);
  49. return (0);}
  50.  
I know gets() is generally bad and know i am missing some code to compleate this just hoping for some derection and clarification.
Mar 28 '08 #1
4 1762
Im a newby to programing 2 months exp.. I am trying to write code in C to take a input string from the user and then the input substring. I wish to compare the substring to the string and count the number of times it apears and print this to the user. After many hours of frustration i have gotten this far any help or derection would be aprieciated
Expand|Select|Wrap|Line Numbers
  1. # include<stdio.h>  
  2. # include<math.h>
  3. # include<stdlib.h>
  4. # include<string.h>
  5.  
  6. int main(void)
  7.  
  8. {
  9. char s1[200], s2[200];
  10. char count;
  11. int count2;
  12. int run;
  13. int i;
  14. int j;
  15. int strstrcnt(const char *s1,const char *s2);
  16. char s[200];
  17.  
  18. printf("\nThis is a program to find certian occurances of words in a user inputed sentence\n");
  19. printf("\nIt will display the number of times the word is found.\n");
  20.  
  21. printf("\nTo run this program and input the word to find and the sentence to search hit 1,\n\n Hit 2 to exit.\n");
  22.  
  23. scanf("%d",&run);
  24.  
  25. fflush(stdin);
  26.  
  27. while (run==1)
  28.  
  29. {printf("\nPlease input the sentence you wish to hae searched.\n");
  30.  
  31.  gets(s1);
  32.  
  33.  printf("\nPlease enter the word or term you wish to have found in the sentence.\n");
  34.  
  35.  gets(s2);
  36.  
  37.  strstrcnt(s1,s2);
  38.  
  39.  printf("%d",s1[0]);
  40.  
  41.  
  42. }
  43.  
  44. return(0);}
  45.  
  46. int strstrcnt(const char *s1,const char *s2)
  47. {
  48. char * strstr(const char *s1, const char *s2);
  49. return (0);}
  50.  
No help yet? can anyone tell me where the return value from the strstr goes and am i able to check to see if it is null or not by if statments? Further does the return value to the array need to be 0 or should it be a pointer to s1 or s2?
Mar 30 '08 #2
No help yet? can anyone tell me where the return value from the strstr goes and am i able to check to see if it is null or not by if statments? Further does the return value to the array need to be 0 or should it be a pointer to s1 or s2?
According to link, strstr returns a pointer to first occurrence of the search word in the given sentence. So you could test something like this:

Expand|Select|Wrap|Line Numbers
  1. char sentence[200], word[200]; 
  2. char* occurs_at; 
  3. int location; 
  4.  
  5. //code to get user inputs for: sentence and word, then:
  6.  
  7. occurs_at = strstr(sentence, word);   //a pointer (to somewhere in sentence)
  8. if(occurs_at)  //if true
  9.    location = occurs_at - sentence;   //because sentence[location] == *occurs_at;
  10. else  //strstr has returned a null in occurs_at
  11.    printf("\nNo match");
  12.  
  13.  
--
Mar 30 '08 #3
You can use the strstr function from the C library. You can also use scanf instead of gets() function. Please refer to any standard text book to know the syntax and usage of these functions.

Here is the code you may want to add

Expand|Select|Wrap|Line Numbers
  1. char *c1 = "Input the matching string"; /*first string entered by user */
  2. char *c2 = "matching string"; /* the string to be matched entered by user */
  3. char *result = strstr(c1, c2);
  4. if (result != NULL)  {
  5. printf("The substring is %s \n", result);
  6. } else {
  7. printf("Could not find the substring %s from %s \n", c2, c1);
  8. }
  9.  
I hope this helps.

Ambrish Kinariwala
Mar 31 '08 #4
Thanks for the help i have it figured out with the suggestions, below is a chunk of the code i needed to add, hope others others can learn from it.
Expand|Select|Wrap|Line Numbers
  1.    char *q;//pointer
  2.    int count=0; for (i=0;i<length;i++)
  3.          q[i]=subcap[i];
  4.       q=strstr(q+1,s2);
  5.    q=strstr(s1,s2);
  6.    while (q!= NULL)
  7.    {q=strstr(q+1,s2);
  8.        count++;}//counter
  9.  
Mar 31 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Srinath Avadhanula | last post: by
4 posts views Thread by Travers Naran | last post: by
7 posts views Thread by Sam Lowry | last post: by
8 posts views Thread by pembed2003 | last post: by
5 posts views Thread by Melissa Cowan | last post: by
28 posts views Thread by Bailey.W87 | last post: by
21 posts views Thread by c | last post: by
3 posts views Thread by nitric | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.