fork download
  1. // Saliha Babar CS1A Chapter 10, #6 , Page 589
  2. //
  3. /*****************************************************************************
  4.  * COUNT VOWELS AND CONSONANTS
  5.  * ___________________________________________________________________________
  6.  * This program accepts a string up to n characters, and calculates the number
  7.  * of vowels and consonants in that string.
  8.  * ___________________________________________________________________________
  9.  * INPUT
  10.  * SIZE : size of the string
  11.  * array : array of characters
  12.  * choice : user choice from the menu
  13.  *
  14.  * OUTPUT
  15.  * vowelsCount : number of vowels
  16.  * consCount : number of consonants
  17.  * ***************************************************************************/
  18. #include <iostream>
  19. #include <cctype>
  20. using namespace std;
  21.  
  22. char* GetString (int SIZE);
  23. int CountVowel (char array[] , int SIZE);
  24. int CountConsonant (char array[] , int SIZE);
  25.  
  26. int main() {
  27. const int SIZE = 31; // INPUT - size of the string
  28. char *array; // Pointer of the array
  29. int choice; // INPUT - User choice for menu selection
  30. int vowelsCount; // OUTPUT - number of vowels
  31. int consCount; // OUTPUT - number of consonants
  32. char input; // User choice to repeat the menu/exit program
  33.  
  34. do {
  35. //Get the string from user
  36. array = GetString (SIZE);
  37.  
  38. // Show the menu to the user
  39. cout << "---------Menu----------\n";
  40. cout << "1- count vowels\n";
  41. cout << "2- count consonants\n";
  42. cout << "3- count consonants and vowels\n";
  43. cout << "Enter your choice\n";
  44. cin >> choice;
  45. cin.ignore();
  46.  
  47. switch (choice) {
  48. case 1 :
  49. vowelsCount = CountVowel(array, SIZE);
  50. cout << "Number of vowel(s) :" << vowelsCount << endl;
  51. break;
  52.  
  53. case 2 :
  54. consCount = CountConsonant (array, SIZE);
  55. cout << "Number of consonants(s) :" << consCount << endl;
  56. break;
  57.  
  58. case 3 :
  59. vowelsCount = CountVowel(array, SIZE);
  60. consCount = CountConsonant (array, SIZE);
  61. cout << "Number of vowel(s) :" << vowelsCount << endl;
  62. cout << "Number of consonants(s) :" << consCount << endl;
  63. }
  64.  
  65. cout << "\nPress any key to continue with different string\n";
  66. cout << "Enter Y to quit the program\n";
  67. cin >> input;
  68.  
  69. delete [] array;
  70. }
  71.  
  72. while (tolower(input) != 'y' && toupper(input) != 'Y');
  73.  
  74. return 0;
  75. }
  76.  
  77. char* GetString (int SIZE)
  78. {
  79. char *character;
  80. character = new char [SIZE];
  81.  
  82. cout << "Enter a string up to " << SIZE -1 << " characters\n";
  83. cin.ignore();
  84. cin.getline (character,SIZE);
  85.  
  86. // Convert those characters to lowercase
  87. for (int i = 0; i < SIZE ; i++)
  88. {
  89. character[i] = tolower (character[i]);
  90. }
  91.  
  92. return character;
  93. }
  94.  
  95.  
  96. int CountVowel (char array[] , int SIZE)
  97. {
  98. int count = 0;
  99. for (int i = 0; i < SIZE ; i++)
  100. {
  101. if (array[i] == 'a' || array[i] == 'e' || array[i] == 'i' ||
  102. array[i] == 'o' || array[i] == 'u')
  103. count++;
  104. }
  105. return count;
  106. }
  107.  
  108. int CountConsonant (char array[] , int SIZE)
  109. {
  110. int count = 0;
  111. for (int i = 0; i < SIZE ; i++)
  112. {
  113. if (isalpha(array[i]) && array[i] != 'a' && array[i] != 'e' && array[i] != 'i' &&
  114. array[i] != 'o' && array[i] != 'u')
  115. count++;
  116. }
  117. return count;
  118. }
Success #stdin #stdout 0s 5292KB
stdin
Saliha Babar
1
d
Saliha 
2
y
stdout
Enter a string up to 30 characters
---------Menu----------
1- count vowels
2- count consonants
3- count consonants and vowels
Enter your choice
Number of vowel(s) :5

Press any key to continue with different string
Enter Y to quit the program
Enter a string up to 30 characters
---------Menu----------
1- count vowels
2- count consonants
3- count consonants and vowels
Enter your choice
Number of consonants(s) :3

Press any key to continue with different string
Enter Y to quit the program