The program takes a string. Using a for loop, the number of spaces in the string are counted. The total number of words will be number of spaces plus 1. The result is printed.
WORD COUNT
Program:
#include<iostream>
using namespace std;
int main()
{
char str[80];
cout<<"Enter the string:";
cin.getline(str,80);
int i,words=1,ch=0;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
{
words++;
}
else
{
ch++;
}
}
cout<<"Number of words:"<<words<<endl;
cout<<"Number of characters:"<<ch<<endl;
return 0;
}
OUTPUT:
BY
REGU RAM SV.
إرسال تعليق