Answer to Question #190032 in C++ for jainab

Question #190032

Write a program in C++ to count the words “this” and “these” present in a text file “ARTICLE.TXT”. Use file handling concept.


1
Expert's answer
2021-05-06T12:05:45-0400
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main(){
    fstream file;
    string input;
    istringstream iss;
    int thisCount = 0, theseCount = 0;
    file.open("ARTICLE.TXT", ios::in);
    if(file) while(getline(file, input)){
        iss = istringstream(input);
        while(iss>>input){
            if(input[input.length() - 1] < 65) input = input.substr(0, input.length() - 1); //remove !, ., ?
            if(input == "this" || input == "This") thisCount++;
            else if(input == "these" || input == "These") theseCount++;
        }
    }
    else{
        cout<<"Error opening file!";
        return -1;
    }
    file.close();
    cout<<"Number of ""this"": "<<thisCount<<endl;
    cout<<"Number of ""these"": "<<theseCount<<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