Simpl C Programming Counting Words 1030Prob3txtSolutioninclu
Simpl C++ Programming Counting Words
1030Prob3.txt
Solution
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
inputFile.open(\"1030Prob3.txt\");
string s;
int count = 0;
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile >> s;
if(s.at(0) == \'a\' || s.at(0) == \'A\'){
count++;
}
}
}
cout<<\"The count of words starting with \'a\' or \'A\' is \"<<count<<endl;
inputFile.close();
return 0;
}
Output:
sh-4.3$ g++ -std=c++11 -o main *.cpp
sh-4.3$ main The count of words starting with \'a\' or \'A\' is 20
