Answer to Question #98365 in C++ for matimu

Question #98365
Write a C++ program which reads in a line of text and counts the number of occurrences of the word ‘the’ in it. It outputs the line of text on one line, ‘The number of times the ppears is’ and the count on the next line using a single cout statement.
1
Expert's answer
2019-11-13T04:20:45-0500
#include <iostream>
#include <string>


using namespace std;


int main()
{
    string str, str1;                               // Strings for console input.
    int num = 0;                                    // The number of words "the" in the line.
    cout << "Please enter a line of text: " ;
    char c=' ';                                     // Symbol for input correction.


    while((cin.peek()!='\n')&&(c!='\n'))            // Checking the end of the line.
    {
        while(c==' ' || c=='\t')                    // Do not enter spaces and tabs.
        {
            c=cin.get();
        }


        if (c!='\n')                                // If not the end of the line,
        {                                           // then enter the printed characters.
            str.clear();
            str+=c;
            if(cin.peek()!='\n')
            {
              cin>>str1;
              str+=str1;
            }                                       // We're forming a word.
            c=' ';
            //cout<<str<<" ";                       Can be turned on to control input.
            if(str=="the") num++;                   // Checking the word.
        }
    }


    cout<<endl<<"The number of times the ppears is "<<endl<<num<<endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS