Count number of words - C++ Program

Q. Write a C++ program to count total number of words used in any sentence.

Answer:

Following program is counting total numbers of words used in a string.

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
        char strs[100], countw=0, i;
        cout<<"\n Enter String : ";
        gets(strs);
        int len=strlen(strs);
        for(i=0; i<len; i++)
        {
                if(strs[i]==' ')
                {
                        countw++;
                }
        }
        cout<<"\n Total Number of Words in String are : "<<countw+1;
        return 0;
}


Output:

count word