Tuesday 24 June 2014

C++ program to count number of words in a string


Here is a C++ program to count number of words in a string 


 SOURCE CODE OUTPUT
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[100];
    int i,numwrds=0;
    cout<<"Enter String : ";
    cin.getline(str,100);
    for(int i=0;str[i]!='\0';i++)
    {
        if(str[i]==' ')
            numwrds++;
    }
    cout<<"Total words = "<<numwrds+1;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment