Write a program in C++ to count the words “this” and “these” present in a text file “ARTICLE.TXT”. Use file handling concept.
#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;
}
Comments
Leave a comment