// // example of recursion: // function to count alphabetic characters in string, plus test program // #include #include // has EXIT_SUCCESS #include // has setw() #include // has isalpha() // 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; }