C++ function that should count the number of vowels appearing in the string and return that number
Vowels and Consonants
Write a C++ function that accepts a pointer to a C-string as its argument. The function
should count the number of vowels appearing in the string and return that number.
Write another function that accepts a pointer to a C-string as its argument. This function
should count the number of consonants appearing in the string and return that
number.
Demonstrate these two functions in a program that performs the following steps:
1. The user is asked to enter a string.
2. The program displays the following menu:
A) Count the number of vowels in the string
B) Count the number of consonants in the string
C) Count both the vowels and consonants in the string
D) Enter another string
E) Exit the program
Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | // Vowels and Consonants # include <iostream> # include <cstdlib> # include <cctype> using namespace std; // Constant for array size const int SIZE = 81; // Function prototypes int vowels(char *); int consonants(char *); int main() { char choice; // Menu choice char cstring[SIZE]; // To hold a string // Get a string. cout << "Enter a string: " ; cin.getline(cstring, SIZE); // Display the menu. do { cout << "\tA) Count the vowels in the string\n" ; cout << "\tB) Count the consonants in the string\n" ; cout << "\tC) Count both vowels and consonants\n" ; cout << "\tD) Enter another string\n" ; cout << "\tE) Exit this program\n\n" ; // Get the user's choice. cout << "\tEnter A, B, C, D, or E.\n" ; cin >> choice; // Validate the choice. while (toupper(choice) < 'A' || toupper(choice) > 'E' ) { cout << "\tEnter ONLY A, B, C, D, or E.\n" ; cin >> choice; } // Process the user's choice. switch (toupper(choice)) { // Choice A is to count the vowels. case 'A' : cout << "The string has " ; cout << vowels(cstring) << " vowels.\n\n" ; break ; // Choice B is to count the consonants. case 'B' : cout << "The string has " ; cout << consonants(cstring) << " consonants.\n\n" ; break ; // Choice C is to count both vowels and // consonants. case 'C' : cout << "The string has " ; cout << vowels(cstring) << " vowels and " ; cout << consonants(cstring) << " consonants.\n\n" ; break ; // Choice D is to enter another string. case 'D' : cin.get(); cout << "Enter a string: " ; cin.getline(cstring, SIZE); break ; // Choice E is to exit the program. case 'E' : cout << "Goodbye!\n" ; exit (EXIT_SUCCESS); } } while (toupper(choice) != 'E' ); return 0; } //*********************************************************************** // Function vowels * // This function accepts a pointer to a string as its argument. * // It returns the number of vowels in the string. * //*********************************************************************** int vowels(char *str) { char v[] = { 'A' , 'a' , 'E' , 'e' , 'I' , 'i' , 'O' , 'o' , 'U' , 'u' }; int numVowels = 0; while (*str != 0) { for (int count = 0; count < 10; count ++) { if (*str == v[ count ]) { numVowels++; break ; } } str++; } return numVowels; } //*********************************************************************** // Function consonants * // This function accepts a pointer to a string as its argument. * // It returns the number of consonants in the string. * //*********************************************************************** int consonants(char *str) { char v[] = { 'A' , 'a' , 'E' , 'e' , 'I' , 'i' , 'O' , 'o' , 'U' , 'u' }; int numCons = 0, foundVowel; while (*str != 0) { foundVowel = 0; for (int count = 0; count < 10; count ++) { if (*str == v[ count ]) { foundVowel = 1; break ; } } if (!foundVowel) numCons++; str++; } return numCons; } |
Leave a reply