Program to count word in a sentence in C++

bookmark

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main() {
    char str[100];
    int i, len, count = 0;

    cout << "Write a sentence: ";
    gets(str);

    len = strlen(str);

    for (i = 0; i < len; i++) {
        if (str[i] == ' ') {
            count++;
        }
    }

    cout << "Total number of words in a sentence is " << count + 1;

    return 0;
}

 

Output

Input:
Write a sentence: The quick brown fox jumps over the lazy dog.

Output:
Total number of words in a sentence is 9