fork download
  1. /********************************************************************
  2. * Name: Elaine Torrez
  3. * Date: 2/21/2026
  4. * Assignment: HW_5f – Recursive Vowel Counter
  5. *
  6. * Description:
  7. * This program uses recursion to count the number of vowels
  8. * in a c_string (null-terminated character array).
  9. ********************************************************************/
  10.  
  11. /* OUTPUT:
  12. Enter a statement:
  13. Say it ain't so Joe.
  14. There are 7 vowels in the statement.
  15. Press any key to continue . . .
  16. */
  17.  
  18. #include <iostream>
  19. #include <cstring>
  20. using namespace std;
  21.  
  22. // Function prototype
  23. int countVowels(char statement[], int index);
  24.  
  25. int main()
  26. {
  27. // Declare c_string
  28. char statement[20] = ""; // initialized with null
  29.  
  30. // Prompt user
  31. cout << "Enter a statement:" << endl;
  32.  
  33. // Read input (including spaces)
  34. cin.getline(statement, 20);
  35.  
  36. // Call recursive function
  37. int totalVowels = countVowels(statement, 0);
  38.  
  39. // Output result
  40. cout << "There are " << totalVowels
  41. << " vowels in the statement." << endl;
  42.  
  43. cout << "Press any key to continue . . .";
  44. cin.get();
  45.  
  46. return 0;
  47. }
  48.  
  49. // Recursive function to count vowels
  50. int countVowels(char statement[], int index)
  51. {
  52. // Base case: stop at null character
  53. if (statement[index] == '\0')
  54. return 0;
  55.  
  56. // Check if current character is a vowel
  57. char ch = statement[index];
  58.  
  59. if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ||
  60. ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
  61. {
  62. return 1 + countVowels(statement, index + 1);
  63. }
  64. else
  65. {
  66. return countVowels(statement, index + 1);
  67. }
  68. }
Success #stdin #stdout 0s 5328KB
stdin
Say it ain't so Joe.
stdout
Enter a statement:
There are 7 vowels in the statement.
Press any key to continue . . .