Monday, 4 April 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



Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment