How to Count Words of String in C++

 The Program gets Input a String from user and then count the Number of word contain that String. A Condition on space between the sentence is used to accomplish this task.

Source Code 


 #include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main( )
{
 //clrscr( );
 char str[80];
 int words=0;
 cout<<"Enter Your Story to Count Words:";
 gets(str);

 for(int i=0;str[i]!='\0';i++)
 {
  if (str[i]==' ')
   words++;  
 }

 cout<<"Your Story Consist of  ="<<words+1 <<" Words"<<endl;

 getch();
 return 0;
}

Output of the Program

C++ String Programming



Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment