Write a C++ program to count number of words in the entered sentence.

Program Code:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
char strs[100], count=0, i, len;
cout<<"Enter the a sentence to count number of words in it is : ";
gets(strs);
cout<<endl;
len=strlen(strs);
for(i=0; i<len; i++)
{
if(strs[i]==' ')
{
count++;
}
}
cout<<"Total number of words in the entered sentence is : ";
cout<<count+1;
return 0;
}


The program output is tested on www.jdoodle.com
Output:
Enter the a sentence to count number of words in it is : Mukesh Rajput Mukesh
Total number of words in the entered sentence is : 3


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

1 comments:

  1. Three alternatives:
    (Unfortunately, blogspot is loosing the indention...)

    #include <iostream>
    #include <iomanip>
    #include <algorithm>
    #include <cctype>
    #include <regex>

    //Test input is: He said,"This is a test".
    //Correct answer is: 6.

    #if 0
    //Assumes words are always separated by only one space.
    int main()
    {
    //Use flush to make sure our prompt doesn't just sit in a buffer.
    std::cout << "Enter a sentence: " << std::flush;
    //Even in C programs, one shouldn't use the gets() function.
    //C++ gives us getline(), which is the best choice here.
    std::string sentence;
    std::getline(std::cin, sentence);
    //No need for endl. It is only a shortcut for: << '\n' << std::flush
    std::cout << '\n';
    //If we're just counting spaces, std::count will do that for us.
    auto count = std::count(sentence.begin(), sentence.end(), ' ');
    std::cout << "Number of words: " << (count + 1) << '\n';
    }
    #endif

    #if 0
    int main()
    {
    //Set the user's requested locale for alnum().
    //So, in a UTF-8 locale, this should work with accented letters and such.
    std::locale::global(std::locale(""));
    std::cout << "Enter a sentence: " << std::flush;
    std::string sentence;
    std::getline(std::cin, sentence);
    std::cout << '\n';
    size_t count = 0;
    //Find the first character that could be part of a word.
    //We define a word as a sequence of alpha or numeric characters.
    auto iter = std::find_if(sentence.begin(), sentence.end(), ::isalnum);
    while (iter != sentence.end()) {
    ++count;
    //Find the next place where we have a word char following a non-word char.
    iter = std::adjacent_find(iter + 1, sentence.end(),
    [](auto a, auto b){
    return (not ::isalnum(a)) and ::isalnum(b);
    });
    }
    std::cout << "Number of words: " << count << '\n';
    }
    #endif

    //Some may find a solution with regex easier to read.
    int main()
    {
    std::locale::global(std::locale(""));
    std::cout << "Enter a sentence: " << std::flush;
    std::string sentence;
    std::getline(std::cin, sentence);
    std::cout << '\n';
    std::regex word_regex("[[:alnum:]]+");
    auto first = std::sregex_iterator(sentence.begin(), sentence.end(), word_regex);
    auto last = std::sregex_iterator();
    std::cout << "Number of words: " << std::distance(first, last) << '\n';
    }

    ReplyDelete