Counting words in a string?

2019-03-02 06:09发布

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 out how to turn the letters that make a word, and count it as 1 word.

My function is:

int wordcount( char word[MAX] ){

    int i, num, counter, j;

    num = strlen( word );
    counter = 0;

    for (i = 0; i < num; i++)
    {
        if (word[i] != ' ' || word[i] != '\t' || word[i] != '\v' || word[i] != '\f')
        {

        }

    }

    return counter;
}

I tried some variations, but the middle part of the if statement is where I am confused. How can I count the number of words in a string? Testing for this tests if the string has multiple spaces like "Hello this is a string"

标签: c parsing
4条回答
你好瞎i
2楼-- · 2019-03-02 06:46

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.

查看更多
等我变得足够好
3楼-- · 2019-03-02 06:50

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)

查看更多
▲ chillily
4楼-- · 2019-03-02 07:00

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

查看更多
forever°为你锁心
5楼-- · 2019-03-02 07:00

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.

查看更多
登录 后发表回答