// // example of recursion: // // function to count whitespace in string // // main (test) program repeatedly prompts for input // #include #include // precondition: s contains a C-style string and n <= strlen(s) // postcondition: returns the number of whitespace characters in s, // starting with s[n] int countWS(const int n, const char s[]); // ---- main program ---- int main() { string s; char prompt[] = "Enter a line of text, or ctrl-D to end:\n"; cout << prompt; while (getline(cin, s)) { cout << "That line contains " << countWS(0, s.c_str()) << " whitespace characters.\n"; cout << prompt; } return 0 ; } // ---- function definition ---- int countWS(const int n, const char s[]) { if (s[n] == '\0') return 0; else { if (isspace(s[n])) return 1 + countWS(n+1, s); else return countWS(n+1, s); } }