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.
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.
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:-
You May Also See
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
0 Comments: