Question

#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

#include <iostream>
#include <cstring>
#include <string>
#include <istream>

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"<<endl;
       cout << "   (B) Count the number of Consonants in the string"<<endl;
       cout << "   (C) Count both the vowels and consonants in the string"<<endl;
       cout << "   (D) Enter another string" << endl;
       cout << "   (E) Exit program" << endl;

       //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;
}

the code works. the problem here is i dont want to have a set limit on the char array. (my cstring)..instead of just increasing the array size to 1000 and wasting space is there away where i can dynamically allocate an array with the char array. keep in mind "I DO NOT WANT TO JUST INCREASE THE ARRAY SIZE" thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please follow the changes (highlighted in below code)made according to your use.

#include <iostream>
#include <cstring>
#include <string>
#include <istream>
#include <stdlib.h>

using namespace std;

//Function prototypes
int numVowels(char *str);
int numConsonants(char *str);

int main()
{
/*Modify your char string[100] to char *string*/
char *string;
/*dynamic allocation of memory to string array*/
string=(char *) malloc(sizeof(char));
char inputChoice, choice[2];
int vowelTotal, consonantTotal;

/*New variables used*/
int i=0;
char ch;

//Input a string
cout << "Enter a string: " << endl;

/*Instead of using getline, read the char by char till newline char \n and store in array string*/
//cin.getline(string, 100);
while((ch=getchar())!='\n') {
realloc(string, (sizeof(char)));
string[i++]=ch;
}

/*Every string end with null char '\0'*/
string[i] = '\0';

do
{
//Displays the Menu
cout << " (A) Count the number of vowels in the string"<<endl;
cout << " (B) Count the number of Consonants in the string"<<endl;
cout << " (C) Count both the vowels and consonants in the string"<<endl;
cout << " (D) Enter another string" << endl;
cout << " (E) Exit program" << endl;

//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

i=0;/*this i=0 will deallocates the string, and removes the array contents*/
cout << "Enter a string: " << endl;
while((ch=getchar())!='\n') {
realloc(string, (sizeof(char)));
string[i++]=ch;
}
string[i] = '\0';


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;
}

Output:

Enter a string:
abdcdef
(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 program
Enter choice: A
Number of vowels is: 2
(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 program
Enter choice: B
Number of Consonants is: 5
(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 program
Enter choice: C
Number of vowels is: 2
Number of Consonants is:5
(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 program
Enter choice: D
Enter a string:
appleu
(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 program
Enter choice: A
Number of vowels is: 3
(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 program
Enter choice: C
Number of vowels is: 3
Number of Consonants is:3
(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 program
Enter choice: E


Code pics:

File Edit Selection View Go Debug Terminal Help capp-Visual Studio Code 茫include #include #include <iostream> «string> <strin

File Edit Selection View Go Debug Terminal Help capp-Visual Studio Code /*Every string end with nul 1 char 。 string[i] / 35 |File Edit Selection View Go Debug Terminal Help capp-Visual Studio Code 目欲 63 64 65 case C://Function call to get number ofFile Edit Selection View Go Debug Terminal Help capp-Visual Studio Code 目欲 89 90 //End main 91 /Function Definitions 92 int n

Please rate the solution, your rating is precious :)

Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT