help with counting substings in strings???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandvet03
    New Member
    • Mar 2008
    • 4

    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 [code=c]
    # include<stdio.h >
    # include<math.h>
    # include<stdlib. h>
    # include<string. h>

    int main(void)

    {
    char s1[200], s2[200];
    char count;
    int count2;
    int run;
    int i;
    int j;
    int strstrcnt(const char *s1,const char *s2);
    char s[200];

    printf("\nThis is a program to find certian occurances of words in a user inputed sentence\n");
    printf("\nIt will display the number of times the word is found.\n");

    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");

    scanf("%d",&run );

    fflush(stdin);

    while (run==1)

    {printf("\nPlea se input the sentence you wish to hae searched.\n");

    gets(s1);

    printf("\nPleas e enter the word or term you wish to have found in the sentence.\n");

    gets(s2);

    strstrcnt(s1,s2 );

    printf("%d",s1[0]);


    }

    return(0);}

    int strstrcnt(const char *s1,const char *s2)
    {
    char * strstr(const char *s1, const char *s2);
    return (0);}
    [/code]
    I know gets() is generally bad and know i am missing some code to compleate this just hoping for some derection and clarification.
  • sandvet03
    New Member
    • Mar 2008
    • 4

    #2
    Originally posted by sandvet03
    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 [code=c]
    # include<stdio.h >
    # include<math.h>
    # include<stdlib. h>
    # include<string. h>

    int main(void)

    {
    char s1[200], s2[200];
    char count;
    int count2;
    int run;
    int i;
    int j;
    int strstrcnt(const char *s1,const char *s2);
    char s[200];

    printf("\nThis is a program to find certian occurances of words in a user inputed sentence\n");
    printf("\nIt will display the number of times the word is found.\n");

    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");

    scanf("%d",&run );

    fflush(stdin);

    while (run==1)

    {printf("\nPlea se input the sentence you wish to hae searched.\n");

    gets(s1);

    printf("\nPleas e enter the word or term you wish to have found in the sentence.\n");

    gets(s2);

    strstrcnt(s1,s2 );

    printf("%d",s1[0]);


    }

    return(0);}

    int strstrcnt(const char *s1,const char *s2)
    {
    char * strstr(const char *s1, const char *s2);
    return (0);}
    [/code]
    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?
    Last edited by Ganon11; Mar 30 '08, 09:41 PM. Reason: Fixing [CODE] tags.

    Comment

    • Natasha26
      New Member
      • Mar 2008
      • 12

      #3
      Originally posted by sandvet03
      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:

      Code:
      char sentence[200], word[200]; 
      char* occurs_at; 
      int location; 
      
      [i]//code to get user inputs for: sentence and word, then:[/i]
      
      occurs_at = strstr(sentence, word);   [i]//a pointer (to somewhere in sentence)[/i]
      if(occurs_at)  [i]//if true[/i]
         location = occurs_at - sentence;   [i]//because sentence[location] == *occurs_at;[/i]
      else  [i]//strstr has returned a null in occurs_at[/i]
         printf("\nNo match");
      --

      Comment

      • Ambrish Kinariwala
        New Member
        • Mar 2008
        • 6

        #4
        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

        [code=c]
        char *c1 = "Input the matching string"; /*first string entered by user */
        char *c2 = "matching string"; /* the string to be matched entered by user */
        char *result = strstr(c1, c2);
        if (result != NULL) {
        printf("The substring is %s \n", result);
        } else {
        printf("Could not find the substring %s from %s \n", c2, c1);
        }
        [/code]

        I hope this helps.

        Ambrish Kinariwala

        Comment

        • sandvet03
          New Member
          • Mar 2008
          • 4

          #5
          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.
          [code=c]
          char *q;//pointer
          int count=0; for (i=0;i<length;i ++)
          q[i]=subcap[i];
          q=strstr(q+1,s2 );
          q=strstr(s1,s2) ;
          while (q!= NULL)
          {q=strstr(q+1,s 2);
          count++;}//counter
          [/code]

          Comment

          Working...