I have assignment for class to write a code, that will count the number of appearances of a specific word in a .txt
file case insensitive
int main(){
char U[50];
string a;
int number=0;
cout<<"name of file"<<endl;
cin.getline(U,50);
ifstream text(U,ios_base::binary);
if(!text){
cout<<"nonexisting"<<endl;
return 0;
}
cin>>a;
transform(a.begin(), a.end(), a.begin(), ::tolower);
string word;
while(text>>word){
transform(word.begin(), word.end(), word.begin(), ::tolower);
if(!a.compare(word))number++;
}
cout<<number;
text.close();
return 0;
}
The problem is program counts 32 words in a file but there are 40 of them
here is my solution to the problem
int main(){
char U[50];
string a;
int number=0;
cout<<"name of file"<<endl;
cin.getline(U,50);
ifstream text(U);
if(!text){
cout<<"nonexisting"<<endl;
return 0;
}
cin>>a;
transform(a.begin(), a.end(), a.begin(), ::tolower);
string word;
while(text>>word){
transform(word.begin(), word.end(), word.begin(), ::tolower);
if (word.find(a) != string::npos)number++;
}
cout<<number;
text.close();