// Author: Keith Shomper // Date: 9/26/03 // Purose; To demonstrate the for loop, review arrays, demonstrate iomanip, and // demonstrate nested loops by counting the letters in a character array #include #include #include using namespace std; int main() { const int SIZE = 80; int cnt = 0, pos; char phrase[SIZE]; noskipws(cin); // prompt the user for a phrase cout << "What phrase would you like to search on for letters?\n--> "; for (int i = 0; i < SIZE; i++) { cin >> phrase[i]; if (phrase[i] == '\n') { break; } } // Note the "I", "T" and "M" of ITEM all happen in the next line for (char c = 'a'; c <= 'z'; ++c ) { // initialize the while loop control variable pos = 0; // get a count of the current letter, c while (pos != SIZE) { // attempt to find the letter in the string while (pos < SIZE && phrase[pos++] != c); if (pos == SIZE) { ; // do nothing, we're getting ready to exit the loop } else { ++cnt; // count the letter ++pos; // start the new search at the next letter } } // report how many times the current letter appears and reset the count if (cnt > 0) { cout << "The letter " << c << " appears " << cnt << " time(s)." << endl; cnt = 0; } // The upper and lower case alphabet is split in the ASCII code by the // symbols '[', '\', ']', '^', '_' and '`', so when we get to 'Z', we // need to advance the current character forward to '`', so the next // character will be an 'a' if (c == 'Z') c = '`'; } return 0; }