Count Words

Lab C Intermediate 20 points

Read a line and print the number of words (groups of non-space characters, any number of spaces between).

Input

One line.

Output

The word count.

Example

Input

the quick  brown   fox

Output

4
Sample input available Sample output available
Editor
C

Hints

Count the moments you go from a space into a non-space.

Solution explanation

A small state machine (in a word or not) handles repeated spaces correctly.

Show solution
#include <stdio.h>
#include <string.h>

int main() {
    char s[300];
    fgets(s, 300, stdin);
    s[strcspn(s, "\n")] = '\0';
    int words = 0, in_word = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        if (s[i] == ' ') {
            in_word = 0;
        } else if (!in_word) {
            in_word = 1;
            words++;
        }
    }
    printf("%d\n", words);
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD