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 #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, 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!