C++ Program to Count number of Words in String

PROGRAM:

#include<iostream>
using namespace std;

int main()
{

char str[100];
int w=1;
cout<<"Please enter a String: ";
gets(str);

for (int i=0;str[i]!='';i++)
{
if(str[i]==' ')
w++;
}
cout<<"There are "<<w<<" words.";
return 0;
}

 

OUTPUT:

count words in string

Leave a Reply