// // Program: count_space. // // Purpose: test recursive function to count spaces in character // string. // // Input: // a line of text from standard input. // Output: // the number of whitespace characters in the input text, printed // to standard output. #include #include #include // Pre: s contains a null-terminated string; n >= 0 and < length(s). // Post: returns number of whitespace characters in s, starting // from position n. int count_space(char s[], int n) { if (s[n] == '\0') return 0; else if (isspace(s[n])) return 1 + count_space(s, n+1); else return count_space(s, n+1); } // Main program. int main(void) { const int N = 200; char line[N]; cout << "Enter a line of text:\n"; cin.getline(line, N); cout << "That line contains " << count_space(line, 0) << " whitespace characters.\n"; return EXIT_SUCCESS; }