// Program file: strgtest2.cpp // Test strings and string class functions #include #include #include using namespace std; void get_word(string &word); char get_letter(); int count_letter(string s, char ch); char print_count(string s, int c, char ch); int main() { string word; char ch = 'Y'; int count; while(ch != 'N' && ch != 'n') { clrscr(); get_word(word); ch = get_letter(); count = count_letter(word, ch); ch = print_count(word, count, ch); } return 0; } void get_word(string &w) { cout << "Enter the word: "; cin >> w; // getline(cin, w); //p190 string with spaces } char get_letter() { char l; cout << "Enter the character to be counted: "; cin >> l; return l; } int count_letter(string s, char ch) { int j, c = 0; for (j = 0; j < s.size(); ++j) if (s[j] == ch) ++c; cout << "j = " << j << " s.size = " << s.size() << endl; return c; } char print_count(string s, int c, char ch) { cout << "There are " << c << ' ' << ch << "'s in " << s << endl; cout << "Type (Y/N) and press enter to continue: "; cin >> ch; return ch; }