How to Count Words in a String in C++

This is the simple and logic building C++ program to understand the string functions. The program counts the total number of words in a given string.

Source Code 

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
cout<<"\a\n\n\n"
<<"\t******************************************************"<<endl
<<"\t** **"<<endl
<<"\t** Welcome To **"<<endl
<<"\t** **"<<endl
<<"\t** Count words **"<<endl
<<"\t** **"<<endl
<<"\t** Demo **"<<endl
<<"\t** **"<<endl
<<"\t******************************************************"<<endl<<endl<<endl;cout<<"";
string s("Somewhere down the road");
istringstream iss(s);
int wordCount=0;
do
{
string sub;
iss >> sub;
wordCount++;
} while (iss);

cout <<"Total words present in a givent string \""<<s<<"\" is : "<<wordCount-1<<endl;
system("pause");
return 0;
}

Output of the Program

Count Words in a String in C++

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.

2 comments:

  1. William Hill Casino - Ainsworth, MI
    Book William Hill Casino - Ainsworth, MI in the best location betway login with the finest sports betting experience in the country, william hill William 더킹카지노 Hill online casino at

    ReplyDelete
  2. How to register a new bet365 account | Sportsbook - Airjordan 7
    Bet365 is cheap jordan 1 retro one of the best betting sites 토토커뮤니티 in 토토 사이트 the world when it comes to mobile betting. Authentic jordan 7 retro We'll show you how 올인 구조대 you can use the Bet365 app

    ReplyDelete