Question
#include #include #include #include using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal,
#include
using namespace std;
//Function prototypes int numVowels(char *str); int numConsonants(char *str);
int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal;
//Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"< //Inputting choice cout << "Enter choice: "; cin.getline(choice, 2); inputChoice = choice[0]; //a single char variable will now equal the choice user pic from c-string switch (toupper(inputChoice)) //a switch case, any user input char that is lower case will be now uppercase { case 'A'://Function call to get number of Vowels vowelTotal = numVowels(string); cout << "Number of vowels is: " << vowelTotal << endl; break; case 'B': //Function call to get number of consonants consonantTotal = numConsonants(string); //Outputting number of Consonants cout << "Number of Consonants is: " << consonantTotal << endl; break; case 'C': //Function call to get number of Vowels vowelTotal = numVowels(string); //Function call to get number of consonants consonantTotal = numConsonants(string); //Outputting Both Vowels and Consonants cout << "Number of vowels is: " << vowelTotal << endl; cout << "Number of Consonants is:" << consonantTotal << endl; break; case 'D': //Inputting another string //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); break; case 'E': exit(0); break; }//End of Switch } while (inputChoice != 'E');//End Do while }//End main //Function Definitions int numVowels(char *str) { int count = 0;//Local variable accumulates the total number of vowels while (*str != '\0') { if (*str == 'a' || *str == 'e' || *str == 'i' || *str == 'o' || *str == 'u') count++; str++; }//End While //Returns count value to function call return count; } int numConsonants(char *str) { int count = 0; //Checks for all characters in C-string while (*str != '\0') { //Checks for consonants if (*str != 'a' && *str != 'e' && *str != 'i' && *str != 'o' && *str != 'u') count++; str++; }//End While //Returns count value to function call return count; } This is the code to my homework probably. the Program works but the problem is that if i enter more than 100 chars my program will crash because of my array setting. how would i set it to where i can input any amount of chars and wouldn't worry about the set limit. is there away to dynamically allocate memory for cstrings? Thank you.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started