Counting words in a string?

后端 未结 4 467
被撕碎了的回忆
被撕碎了的回忆 2021-01-23 07:32

Hello for this program I am supposed to count the number of words in a string. So far, I have found out how to find the number of characters in a string but am unable to figure

相关标签:
4条回答
  • 2021-01-23 07:38

    Hints only since this is probably homework.

    What you're looking to count is the number of transitions between 'word' characters and whitespace. That will require remembering the last character and comparing it to the current one.

    If one is whitespace and the other is not, you have a transition.

    With more detail, initialise the lastchar to whitespace, then loop over every character in your input. Where the lastchar was whitespace and the current character is not, increase the word count.

    Don't forget to copy the current character to lastchar at the end of each loop iteration. And it should hopefully go without saying that the word count should be initialised to 0.

    0 讨论(0)
  • 2021-01-23 07:45

    When you're in the if part, it means you're inside a word. So you can flag this inword and look whether you change from out of word (which would be your else part) to inword and back.

    0 讨论(0)
  • 2021-01-23 07:53

    There is a linux util 'wc' that can count words.

    have a look (it includes some explanation and a sample):

    http://en.literateprograms.org/Word_count_(C)

    and a link to the source

    http://en.literateprograms.org/index.php?title=Special:DownloadCode/Word_count_(C)&oldid=15634

    0 讨论(0)
  • 2021-01-23 07:54

    This is a quick suggestion — there could be better ways, but I like this one.

    First, be sure to "know" what a word is made of. Let us suppose it's made of letters only. All the rest, being punctuation or "blanks", can be considered as a separator.

    Then, your "system" has two states: 1) completing a word, 2) skipping separator(s).

    You begin your code with a free run of the skip separator(s) code. Then you enter the "completing a word" state which you will keep until the next separator or the end of the whole string (in this case, you exit). When it happens, you have completed a word, so you increment your word counter by 1, and you go in the "skipping separators" state. And the loop continue.

    Pseudo C-like code:

    char *str;
    
    /* someone will assign str correctly */
    
    word_count = 0;
    state = SKIPPING;
    
    for(c = *str; *str != '\0'; str++)
    {
        if (state == SKIPPING && can_be_part_of_a_word(c)) {
            state = CONSUMING;
            /* if you need to accumulate the letters, 
               here you have to push c somewhere */
        }
        else if (state == SKIPPING) continue; // unneeded - just to show the logic
        else if (state == CONSUMING && can_be_part_of_a_word(c)) {
            /* continue accumulating pushing c somewhere 
               or, if you don't need, ... else if kept as placeholder */
        }
        else if (state == CONSUMING) {
            /* separator found while consuming a word: 
               the word ended. If you accumulated chars, you can ship
               them out as "the word" */
            word_count++;
            state = SKIPPING;
        }
    }
    // if the state on exit is CONSUMING you need to increment word_count:
    // you can rearrange things to avoid this when the loop ends, 
    // if you don't like it
    if (state == CONSUMING) { word_count++; /* plus ship out last word */ }
    

    the function can_be_part_of_a_word returns true if the read char is in [A-Za-z_] for example, false otherwise.

    (It should work If I have not done some gross error with the abetment of the tiredness)

    0 讨论(0)
提交回复
热议问题