04/04/2016

C++ Program to Count Number of Words (Words Counter) in String

Problem:- Count words in a given string or Program to count number of words in string or C++ Program to count the number of spaces in a given string or Program to count number of words, lines, and characters in given string or C++ Program to Count Number of Words in String or Count words in a user-input string in C++ or C++ PROGRAM TO COUNT THE NUMBER OF WORDS or C++ program to count number of words in a string or C program to find frequency of words in a string.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

Explanation:- c++ word frequency counter is different from the counting the character in a string, as we know that the words are a group of two or more character and words are separated from white space so we have to count the total number of words in a string.
so basically idea is to count the characters between two spaces in a given string or sentence, from starting to next space counter will be 1 and for next space to next to next space increase the counter by one now counter is two and so on until program reached to second last space to NULL character in string or sentence.


Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.


Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page.



Solution:-

#include<iostream>
#include<conio.h>
#include<cstring>
using namespace std;
int main()
{
/*Visit - www.programmingwithbasics.com*/
   
    printf("=====================================");
    printf("\nVisit - www.programmingwithbasics.com");
    printf("\n=====================================");
    
char str[100],a;
int w=0,l,c=0,j, i;

cout<<"\nPlease Enter A String: \n\n";
gets(str);

for(i=0;i<100;i++)
    if(str[i]==' ')
      c++;
    else
        break;

for(j=c;str[j]!='\0';j++)
{
if(str[j]==' ' && str[j+1]!=' ')
w++;
}

if(str[j-1]==' ')
  w--;
cout<<w+1;

return 0;
}

Output:-

C++ Program to Count Number of Words (Words Counter) in String

You May Also See


1. Hacker Rank solution for Strings

2. Hacker Rank solution for StringStream

3. Hacker Rank solution for Compare the Triplets

4. Hacker Rank solution for A Very Big Sum

5. Hacker Rank solution for Diagonal Difference

6. Hacker Rank solution for Plus Minus

7. Hacker Rank solution for Staircase

8. Hacker Rank solution for Mini-Max Sum

9. Hacker Rank solution for Simple Array Sum

10. Hacker Rank solution for Solve Me First



Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: