// // Program: count-alpha // // Purpose: print input in reverse order // // Input: sequence of numbers x1, x2, .. xN, from standard input. // // Output: x1, x2, .. xN printed to standard output in reverse order // (xN, .... x2, x1). // // // example of recursion: // function to count alphabetic characters in string, plus test program // // test program input: character string S. // test program output: number of alphabetic characters in S. #include #include // has EXIT_SUCCESS #include // has setw() #include // has isalpha() using namespace std; // Function to count alphabetic characters in string. // Pre: s contains a null-terminated string and n <= strlen(s) // Post: returns the number of alphabetic characters in s, // starting with s[n] int countAlpha(const int n, const char s[]) { if (s[n] == '\0') return 0; else if (isalpha(s[n])) return 1 + countAlpha(n+1, s); else return countAlpha(n+1, s); } // Main program. int main(void) { const int BUFFSIZE = 1000; char instring[BUFFSIZE]; cout << "Enter a character string:\n"; // setw() guards against overrunning end of "instring" array. cin >> setw(BUFFSIZE) >> instring; cout << '"' << instring << '"' << " contains " << countAlpha(0, instring) << " alphabetic characters\n"; return EXIT_SUCCESS; }