C++ Program to Count Number of Words in a String
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50];
int count = 0, i;
cout << "Enter a string : ";
gets(str);
for (i = 0; str[i] != '\0';i++)
{
if (str[i] == ' ')
count++;
}
cout << "Number of words in the string are: " << count + 1;
return 0;
}
Output:
Enter a string : Mosaic
Number of words in the string are : 1
