Thread: Need help on finding word count

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    @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...
    no sh it really????? get a grip on life here, that is just stupid nit picking. un realist nit picking here as far as I see. I know I am not going to put in over 300 chars to test it. so slow down.

    the OP has his for 1000 the same can be said about his too. if it is 1001 it is truncated, oh my lets all of you get a grip on yourselves. One programs it to the size they allow to be used. I decided to use 300 because I know I am not about to type in anything over 300 or even close to 300. that is a good ball park number for my purpose. thank you very much.


    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.
    yes you did. so now I got another function for checking that. but as I stated in my other post in here to Mr ocpd perfect ... I am not going to repeat it.


    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.
    I never said it was and that is your option I just grabbed a thought, it was. get each word separately then check it against the search word, and it does that. does it have room for improvement> I do not care at this moment in my life. It was just something I did to see if I could. and I accomplished it. time to move on. it is and was not intended for the OP.

    my code was not even intended for the OP I was doing nothing to it other then stating what I found. period. the function I wrote was me just trying to figure out a different way for doing it. and nothing more. It was never intended to be used in anything. period. I just posted it.


    Your solution does too much. It's just not necessary to do it the way that you are

    really? are you serious? it removes each word one at a time looks at it against the other word if match count if not don't count I guess I could have had it look at each word by one letter at a time, then it too can be said that it does too much and is not necessary as well.

    mine works. it does exactly what I wrote it to do. so yes it is good code.

    is there another way to grab each word separately? I am sure there is and if so then woo hoo for that too.
    Last edited by userxbw; 11-13-2017 at 07:00 PM.

  2. #17
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Reading word by word from text file in C

    char - Reading word by word from text file in C - Stack Overflow


    a lot more code going on to do what I did. to read one word at a time there

    answers one and two.

  3. #18
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Your function doesn't even work even if the input size is within limits. I thought I showed you:

    Code:
    ./a.out 
    Enter sentence
    It doesn't work
    enter word to find many times it shows up
    work
    Search word [ work ] found 0 times
    I'm pretty sure that 0 is not the correct answer

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Here's an example of what I meant. Note that there are still problems - like, because of how this works, asingle word sentence is only matched if there is a deliberate space put after the word. You can make this smarter if you want (words can end with punctuation as well) but that is left as an exercise.

    Code:
    void find_freqs(char sentence[], char word[])
    {
        int i, j, frequency;
        size_t snum, wnum;
        
        snum = strlen(sentence);
        wnum = strlen(word);
        frequency = 0;
    
        for (i = 0; i < snum; i++)
        { 
            // Look for first character of word, taking care to find surrounding spaces.
            // If the matching word is the first word in the sentence, then we should make
            // certain that the char after the word is a space.
            if (sentence[i] == word[0] && sentence[i > 0? i - 1 : wnum] == ' ')
            {
                // i + j should be safe, so we check against the length of sentence.
                for (j = 0; j < wnum && i + j < snum; j++)
                {
                    if (sentence[i + j] != word[j])
                    {
                        break;
                    }
                }
                if (j == wnum)
                {
                    frequency++;
                }
            }
        }
        printf("The string '%s' occurs %d times \n", word, frequency);
    }

  5. #20
    Banned
    Join Date
    Aug 2017
    Posts
    861
    here is even faster way then OP or me for the same thing.

    strstr to find a word in a string using c returning true all the time - Stack Overflow

    woohoo I discovered a new function strike up the band.

    and his is only using 100 chars for each array you better go contact him and tell him what you enlightened me in on about that. gezzz
    Last edited by userxbw; 11-13-2017 at 07:17 PM.

  6. #21
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    My problem, userxbw, is that you presented your buggy code to the OP as if it was a ready-to-go solution.
    whiteflags is not the OP, if he or she were to use it oh well. for my intentions it is ready to go, I am done with it and will never not once put it to use.

    for theirs, if not the not. they are to do what? test the code. do you buy a ready to go car without looking it over or testing it too? whatever I am done justifying myself to

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    strstr() will find any substring... We're back to finding all the I's in "I like pIzza and pIneapples." again...

    You really don't care how you answer, even if your answer introduces new problems. That really ........ing sucks.

  8. #23
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    Your function doesn't even work even if the input size is within limits. I thought I showed you:

    Code:
    ./a.out 
    Enter sentence
    It doesn't work
    enter word to find many times it shows up
    work
    Search word [ work ] found 0 times
    I'm pretty sure that 0 is not the correct answer
    oh my my because It is not programmed to look for ' in a sentence I wasn't looking for it, so it works the way I wrote it to work. therefor it works.

    again I did not intend it for anyone other then me and it is never ever not even once going to be used so I will find a matching word in a sentence it is just going to sit on my hard drive until I delete it. but it still works to do what i wrote it to do ocpd dude.

  9. #24
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    strstr() will find any substring... We're back to finding all the I's in "I like pIzza and pIneapples." again...

    You really don't care how you answer, even if your answer introduces new problems. That really ........ing sucks.
    again it was never intended for the OP PERIOD it is crap code even if it did work to your liking and ocpd's liking because I am never going to use it. PERIOD.

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by userxbw View Post
    oh my my because It is not programmed to look for ' in a sentence I wasn't looking for it, so it works the way I wrote it to work. therefor it works.
    For what it's worth, the problem is actually that if a space character doesn't follow the last word, flaged won't be made 0 and check_word won't be terminated. So, the last word isn't compared at all. Which is why you can make endless examples of the function not working just by looking for the last word in a sentence.

    It's the same problem as my function.

    You can fix it by looking for '\0' too.

    again I did not intend it for anyone other then me and it is never ever not even once going to be used so I will find a matching word in a sentence it is just going to sit on my hard drive until I delete it. but it still works to do what i wrote it to do ocpd dude.
    You get really defensive when someone finds an example of your code not working, but you could handle it better. All that it means is that you didn't debug your code that well. Get a grip, as you like to say to me.

  11. #26
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    @userxbw

    Will you stop being defensive and look at the evidence?

    Code:
    > ./a.out 
    Enter sentence
    the function is broken
    enter word to find many times it shows up
    broken
    Search word [ broken ] found 0 times
    Do you see any characters that your function was not designed for, like '? No. The function is broken and I told you this 4 hours ago but you chose to gloss over the fact. Oh wait... maybe my compiler and brain are both broken and the function works after all.

  12. #27
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    @userxbw

    Will you stop being defensive and look at the evidence?

    Code:
    > ./a.out 
    Enter sentence
    the function is broken
    enter word to find many times it shows up
    broken
    Search word [ broken ] found 0 times
    Do you see any characters that your function was not designed for, like '? No. The function is broken and I told you this 4 hours ago but you chose to gloss over the fact. Oh wait... maybe my compiler and brain are both broken and the function works after all.
    that it why I added +1 to len to make sure it got the last of it.
    Code:
    userx@slackwhere:~/bin
    $ ./count_words
    Enter sentence
    i want i   
    enter word to find many times it shows up
    i
    Search word [ i ] found 2 times
    edit for all purposes and intent all I was doing is showing her that I already did that '\0' part of it, and the rest of what I said about that function. I never intended it to be OP code or go any further because I am never going to use it. so I did not test it any further because I got others things to do. and as you seen that worked, done, put it away and move on to doing something else.

    if I was going to put it into a program of mine then that is a completely different story. because I try to break my program so I can find what needs fixing and (more) were error checking needs to be.
    Last edited by userxbw; 11-13-2017 at 08:28 PM.

  13. #28
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I bet you think you're clever.
    Code:
    C:\Users\jk\Desktop>sandbox
    Enter sentence
    i want i
    enter word to find many times it shows up
    i
    Search word [ i ] found 1 times
    Just keeping you honest.

  14. #29
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    I bet you think you're clever.
    Code:
    C:\Users\jk\Desktop>sandbox
    Enter sentence
    i want i
    enter word to find many times it shows up
    i
    Search word [ i ] found 1 times
    Just keeping you honest.
    it worked every time I tried it 3 or 4 times good enough for me . I put it away because it is not my home work I do not care if it completely works or not, so sue me for not having a team of testers to check my code. so it is not completely tested obviously for the reasons also stated in post 27 edit part. I was just showing you the '\0' part. I gezzzz

    and oh my I didn't check it first. again it is and was not intended to OP copy turn it in for a grade get an A go to next class .

  15. #30
    Banned
    Join Date
    Aug 2017
    Posts
    861
    and FYI whiteflags

    you're doing exactly what you've been after me NOT to do. give the answer to the question to the OP.
    post #19
    so you just turned yourself into a what?

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