Write a c++ program to count number of words in string.


Write a c++  program to count number of words in string.
Write c++ code to count no. of words in string.
Count number of words in String using c++ code.

 
 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
 
int main( )
{
        clrscr( );
        char str[80];
        int words=0;
        cout<<"Enter a string:";
        gets(str);
 
        for(int i=0;str[i]!='\0';i++)
        {
               if (str[i]==' ')
                       words++; //Checking for spaces
        }
 
        cout<<"The number of words="<<words+1<<endl;
 
        getch();
        return 0;
}



No comments:

Post a Comment