Thread: Need help on finding word count

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    2

    Need help on finding word count

    I need to find how many times a word is repeated in a sentence.
    I'm almost done but there is a minor issue that I don't know how to fix.
    If I input a sentence like "I LIke pIneapples and pIzzas" and search for "I", the output becomes 3 instead of the intended 1.
    How do I fix this? The code I have is below.
    Code:
    
    #include <stdio.h>
    #include "assignment.h"
    #include <mem.h>void ex4() {
        char sentence[1000], word[100];
        int snum, wnum, frequency = 0, i, j;
        puts("Enter Sentence");
        gets(sentence);
        puts("Enter word to find its frequency");
        gets(word);
    
        snum = strlen(sentence);
        wnum = strlen(word);
    
        for (i = 0; i < snum; i++)
        {
            if (sentence[i] == word[0])
            {
                for (j = 0; j < wnum; j++)
                {
                    if (sentence[i + j] != word[j])
                    {
                        break;
                    }
                }
                if (j == wnum)
                {
                    frequency++;
                }
            }
        }
        printf("The string '%s' occurs %d times \n", word, frequency);
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,101
    You need to provide a small fully working program that we can compile and test. Also show what is in assignment.h

    Please use fgets() or scanf() and stop using gets().

    mem.h seems to be a non-standard Windows only header file.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    try using strlen to get only one letter in the word string. You'd have to break it down to each word check its length then use that along with it what you are doing to insure you're not getting same letter matches from a letter within a word in the sentence against the single letter word(s) you're using to search for.

    mod: another thought, strcmp ?
    Last edited by userxbw; 11-13-2017 at 11:08 AM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I am not seeing it. another thing no one properly spells a sentence using cap letters within a word after the first letter. that would classify as user error and I'd personally not program for that. other then to maybe to let them know.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void find_freqs(char sentence[], char word[])
    {
         
      //  char sentence[1000], word[100];
        int snum, wnum, frequency = 0, i, j;
       /*
        puts("Enter Sentence");
        gets(sentence);
        puts("Enter word to find its frequency");
        gets(word);
    */
        snum = strlen(sentence);
        wnum = strlen(word);
    
        for (i = 0; i < snum; i++)
        { 
            if (sentence[i] == word[0])
            {  
                for (j = 0; j < wnum; j++)
                {
                    if (sentence[i + j] != word[j])
                    {
                        break;
                    }
                }
                if (j == wnum)
                {
                    frequency++;
                }
            }
        }
        printf("The string '%s' occurs %d times \n", word, frequency);
    }
    
    int main (void) 
    {
        
        char words[300];
        char word[100];
         
        
        printf("Enter sentence\n");
        fgets(words, 300,stdin);
         
        printf("enter word to find many times it shows up\n");
        fgets(word, 100, stdin);
        
        find_freqs(words,word);
        
        
    return 0;    
    }
    results:
    Code:
    userx@slackwhere:~/bin
    $ ./count_words
    Enter sentence
    I LIke pIneapples and pIzzas
    enter word to find many times it shows up
    I
    
    The string 'I
    ' occurs 0 times
    so if you got other hidden code somewhere that gets you the results you're taking about I'd love to see it.
    Last edited by userxbw; 11-13-2017 at 11:29 AM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    userxbw,

    Consider removing newlines that fgets() leaves in the string so that they do not affect your results.

    Code:
    C:\Users\jk\Desktop>gcc -std=c99 -Wall sandbox.c -ggdb -o  sandbox
    
    C:\Users\jk\Desktop>gdb sandbox
    GNU gdb (GDB) 7.9.1
    Copyright (C) 2015 Free Software Foundation, Inc.
    [ ... ]
    Reading symbols from sandbox...done.
    (gdb) run
    Starting program: C:\Users\jk\Desktop\sandbox.exe
    [New Thread 6320.0x22d4]
    [New Thread 6320.0x1604]
    Enter sentence
    I like pIzzas and pIneapples.
    enter word to find many times it shows up
    I
    The string 'I' occurs 3 times
    [Inferior 1 (process 6320) exited normally]
    (gdb) q
    
    C:\Users\jk\Desktop>more sandbox.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void find_freqs(char sentence[], char word[])
    {
    
      //  char sentence[1000], word[100];
        int snum, wnum, frequency = 0, i, j;
       /*
        puts("Enter Sentence");
        gets(sentence);
        puts("Enter word to find its frequency");
        gets(word);
    */
        snum = strlen(sentence);
        wnum = strlen(word);
    
        for (i = 0; i < snum; i++)
        {
            if (sentence[i] == word[0])
            {
                for (j = 0; j < wnum; j++)
                {
                    if (sentence[i + j] != word[j])
                    {
                        break;
                    }
                }
                if (j == wnum)
                {
                    frequency++;
                }
            }
        }
        printf("The string '%s' occurs %d times \n", word, frequency);
    }
    
    int main (void)
    {
    
        char words[300];
        char word[100];
    
    
        printf("Enter sentence\n");
        fgets(words, 300,stdin);
        words[strcspn(words, "\n")] = '\0';    // one of several options
    
        printf("enter word to find many times it shows up\n");
        fgets(word, 100, stdin);
        word[strcspn(word, "\n")] = '\0';   // one of several options
    
        find_freqs(words,word);
    
    
    return 0;
    }

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    userxbw,

    Consider removing newlines that fgets() leaves in the string so that they do not affect your results.
    yep, I did a total rewrite of that OPs code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void find_freqs(char sentence[], char word[])
    {
         
      //  char sentence[1000], word[100];
        int snum, wnum, frequency = 0, i, j;
       /*
        puts("Enter Sentence");
        gets(sentence);
        puts("Enter word to find its frequency");
        gets(word);
    */
        snum = strlen(sentence);
        wnum = strlen(word);
    
        for (i = 0; i < snum; i++)
        { 
            //printf("sentence[i] %c\n", sentence[i]);
        //    printf("word %c\n", word[0]);
            if (sentence[i] == word[0])
            { printf("words matched?\n");
                for (j = 0; j < wnum; j++)
                {
                    if (sentence[i + j] != word[j])
                    {
                        break;
                    }
                }
                if (j == wnum)
                {
                    frequency++;
                }
            }
        }
        printf("The string '%s' occurs %d times \n", word, frequency);
    }
    
    int find_freqs_2(char sentence[], char search_word[])
    {
            int a = 0, b = 0,flaged = 3;
            int len = 0, count_times_matched = 0;
            char check_word[300];
            // chop off \n 
            search_word[strlen(search_word) - 1] = '\0';
                
            len = strlen(sentence);
             
            while (a != len+1)
            {  
                if ( sentence[a] != ' ')
                {
                    check_word[b] = sentence[a];
                    b++;
                }
                else
                {
                    check_word[b] = '\0';
                    flaged = 0;
                }
                
                
                    
                if (flaged == 0)
                { 
                    if ( strcmp(check_word,search_word ) == 0)
                        count_times_matched++; 
                         
                    memset(check_word, 0, sizeof(check_word[0]));
                    b=0;
                    flaged = 1;
                }
            a++;
        }  
            
     return count_times_matched;    
     }
    
    int main (void) 
    {
        
        char words[300];
        char word[100];
        
        printf("Enter sentence\n");
        fgets(words, 300,stdin);
    //    printf("words length %lu\n", strlen(words));
        printf("enter word to find many times it shows up\n");
        fgets(word, 100, stdin);
    //    printf("word length %lu\n", strlen(word));
        
      printf("Search word [ %s ] found %d times\n",word, find_freqs_2(words, word) );
        
        //find_freqs(words,word);
        
        
    return 0;    
    }
    thanks though.seeings how you already gave out the complete answer, I guess it ok for me to post mine (now) hehe
    he may now learn their is more then one way to do things.
    Last edited by userxbw; 11-13-2017 at 05:59 PM.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    @userbxw: And what if there is no '\n' at the end of the string? Hmm? Your program will clobber the final character.

    Why is everyone writing over-complicated solutions these days?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Hodor View Post
    @userbxw: And what if there is no '\n' at the end of the string? Hmm? Your program will clobber the final character.

    Why is everyone writing over-complicated solutions these days?
    Depends on what you mean, I guess. The OP probably doesn't know that the string.h library makes this task easy, and it's probably on purpose.

    After all, a solution with strtok and strcmp() isn't very fun.

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by whiteflags View Post
    Depends on what you mean, I guess. The OP probably doesn't know that the string.h library makes this task easy, and it's probably on purpose.

    After all, a solution with strtok and strcmp() isn't very fun.
    I was referring more to userbx's usual convoluted and ill thought out code really. IMO find_freqs_2() is a train wreck

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    @userbxw: And what if there is no '\n' at the end of the string? Hmm? Your program will clobber the final character.

    Why is everyone writing over-complicated solutions these days?
    from what I read fgets puts a \n t the end so it is there because the docs said it is.
    oops I looked again miss read it, ... so what do you suggest? oh never mind just programmer error

    here one I found on it . not the same one I read though.
    fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s .
    Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer.
    but that aside go ahead and test it and get it get it to screw up, then post your results. Because I could not get it to screw up.

    let me do something to my code and put back something i had in there.
    back:
    Code:
    // chop off \n 
            int len1 = strlen(search_word);
            
            if (search_word[len1 - 1] == '\n')
        //    if ( (strlen(search_word) -1) == '\n' )
                search_word[strlen(search_word) - 1] = '\0';
    that commented one wasn't working and seeings how I was just doing this for my own personal gratification, I was not worried about it, because it is not a production program. that other one that is not commented out I just figured out before posting this. that one looks like it is working.

    so it's just a slip up on my part for not going back over every stinking thing before I posted it as I was just showing the other I what I already had did/done relating to her suggestion.

    but that is a good catch nonetheless.
    Last edited by userxbw; 11-13-2017 at 06:26 PM.

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by userxbw View Post
    but that aside go ahead and test it and get it get it to screw up, then post your results. Because I could not get it to screw up.
    There is no challenge in that. The more important issue is that your function doesn't work anyway as it is now.

    Code:
    ./a.out 
    Enter sentence
    This is X
    enter word to find many times it shows up
    X
    Search word [ X ] found 0 times

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    @userxbw:
    There are times where lines can be longer than what you expect to store. For instance in your program the sentence can be up to 300 characters, which is fine, but that means that a 400 character sentence will be truncated... once by fgets() just returning as much as it can, leaving the rest to be read later, and again by your code, chopping off the last character in sentence whether it is a \n or not. One of the reasons I showed you the method I did is because unlike strlen(), strcspn() returns the highest subscript if it doesn't find anything.

    As far as your code goes, the OP's code could have easily been improved as is... maybe check for ' ' or the first character in the word before starting the inner loop, instead of only looking for the first character in word... this makes it so spaces are taken into account.
    Your solution does too much. It's just not necessary to do it the way that you are.

  13. #13
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Just mentioning also that in the OP's original code,
    Code:
    if (sentence[i + j] != word[j])
    can cause an out-of-bounds read

  14. #14
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    I was referring more to userbx's usual convoluted and ill thought out code really. IMO find_freqs_2() is a train wreck
    oh you I get bitc hed at for showing everything and I get bitc hed ed at for not showing everything. if I missed something due to my own personal whatever you still bitc hed is that to say their seem to be nothing but &$^#es in here, or is it you can please some of the people some of the times but you cannot please everyone all of the time.

    as explained I only did that code for my own personal satisfaction so I do not care if I missed a check on it, because it was my own personal throw away code. so It does not matter for one it was not even intended for the OP of he or she being the OP used it then by your self made laws that is cheating anyways. because it is using someone else code, if I had taken the time out to figure out that one part or put in a check that is only good practice but not a necessary act their is a good 99.9999999999999999999 % chance you or the other or someone in here would have itched at me for giving out completed code. ..

    and your option on my how I coded that is just you trying to feed your low self esteem. I'm sorry your mommy didn't respect you well enough or was it your dad?

  15. #15
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    My problem, userxbw, is that you presented your buggy code to the OP as if it was a ready-to-go solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 08:16 AM
  2. word count help
    By regimental in forum C Programming
    Replies: 7
    Last Post: 03-05-2010, 08:47 AM
  3. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  4. word count
    By gokila in forum C Programming
    Replies: 2
    Last Post: 02-19-2002, 01:35 PM

Tags for this Thread