#include <iostream> #include <fstream> #include <string> using namespace std; int main(int argc, char *argv[]) { if (argc < 2) { cout << "Usage: " << argv[0] << " <input_file>" << endl; return -1; } string s; string chs = ",.!?"; int count = 0; fstream f(argv[1]); while (f) { getline(f, s); for (unsigned int i = 0; i < s.size(); i++) { if (chs.find(s[i]) != string::npos) { count++; } } } cout << "count: " << count << endl; return 0; }
Wednesday, May 5, 2010
C++ exercises: read file with fstream and count punctuation marks
Q: Write a program that reads a text file and prints a count of the number of occurrences of the punctuation marks (.,!?).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment