Question

Write a program that reads a string and outputs the number of lowercase vowels in the...

Write a program that reads a string and outputs the number of lowercase vowels in the string. Your program must contain a function with a parameter of a char variable that returns an int. The function will return a 1 if the char being passed in is a lowercase vowel, and a 0 for any other character. The output for your main program should be:

There are XXXX lowercase vowels in string yyyyyyyyyyyyyyyyyyyyyy

Where XXXX is the count of lowercase vowels and yyyyyyyyyyyyyyyyyy is the string entered by the user.

Here is the original code for Exercise 22:

#include <iostream>
#include <string>

using namespace std;

void countVowels(string str, int& aCt, int& eCt, int& iCt,
                 int& oCt, int& uCt);

int main()
{
    string inputString;

    int aCount = 0;
    int eCount = 0;
    int iCount = 0;
    int oCount = 0;
    int uCount = 0;

    cout << "Enter a string: " << endl;
    getline(cin, inputString);

    countVowels(inputString, aCount, eCount, iCount, oCount, uCount);
   
    cout << "The number of a's: " << aCount << endl;
    cout << "The number of e's: " << eCount << endl;
    cout << "The number of i's: " << iCount << endl;
    cout << "The number of o's: " << oCount << endl;
    cout << "The number of u's: " << uCount << endl;

    return 0;
}

void countVowels(string str, int& aCt, int& eCt, int& iCt,
                 int& oCt, int& uCt)
{
    for (unsigned int i = 0; i < str.length(); i++)
        switch (str.at(i))
        {
        case 'a':
                aCt++;
                break;
        case 'e':
                eCt++;
                break;
        case 'i':
                iCt++;
                break;
        case 'o':
                oCt++;
                break;
        case 'u':
                uCt++;
    }
}

C++ program- thank you

Homework Answers

Answer #1

#include <iostream>
#include <string>

using namespace std;

void countVowels(string str, int& aCt, int& eCt, int& iCt,
                 int& oCt, int& uCt);
int checkvowel(char ch);
int main()
{
    string inputString;

    int aCount = 0;
    int eCount = 0;
    int iCount = 0;
    int oCount = 0;
    int uCount = 0;
    int vowelcount=0;
    int count=0;
    cout << "Enter a string: " << endl;
    getline(cin, inputString);

    countVowels(inputString, aCount, eCount, iCount, oCount, uCount);
      for (unsigned int i = 0; i < inputString.length(); i++)
      vowelcount += checkvowel(inputString.at(i));
      if(aCount>0)
      count++;
      if(eCount>0)
      count++;
      if(iCount>0)
      count++;
      if(uCount>0)
      count++;
    cout<< "There are "<<count<< " lowercase vowels in string "<<inputString<<endl;
   cout<< " Where "<<vowelcount<<" is the count of lowercase vowels and " <<inputString<<" is the string entered by the user."<<endl;
  
    return 0;
}

int checkvowel(char c)
{
    
        switch(c)
        {
        case 'a':
                return 1;
                break;
        case 'e':
           return 1;
              
                break;
        case 'i':
                 return 1;
                break;
        case 'o':
                 return 1;
                break;
        case 'u':
                return 1;
        default : return 0;
               
       }
     
return 0;

}
void countVowels(string str, int& aCt, int& eCt, int& iCt,
                 int& oCt, int& uCt)
{
    for (unsigned int i = 0; i < str.length(); i++)
        switch (str.at(i))
        {
        case 'a':
                aCt++;
                break;
        case 'e':
                eCt++;
                break;
        case 'i':
                iCt++;
                break;
        case 'o':
                oCt++;
                break;
        case 'u':
                uCt++;
    }
}

Know the answer?
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
Similar Questions
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
Please provide answer in the format that I provided, thank you Write a program that prompts...
Please provide answer in the format that I provided, thank you Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. I had asked this before but the solution I was given did not work. #include #include using namespace std; void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sammy";    reverse(name);    cout << name << endl; //should display...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Write a C++ program to run a menu-driven program with the following choices: Compute the factorial...
Write a C++ program to run a menu-driven program with the following choices: Compute the factorial of a number Compute the alternating factorial of a number Quit #include <iostream> using namespace std; void getValidUserInputPosNumGT0 (int *a) { int num; cout << "Enter in a positive number greater than 0... "; cin >> *a; } long double factorial (int num) { int fact = 1; while (num > 1) { fact *= num; num--; } return fact; } long double AlternatingFactorial...
Vowels and consonants Write a program that reads a word and prints the number of vowels...
Vowels and consonants Write a program that reads a word and prints the number of vowels and consonants in the word. For this exercise assume that ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, and ‘y’ are vowels. For example, if the user enters the input “Harry”, the program should print “The word contains 2 vowels and 3 consonants”.
Analyze the following program and write down the output. # include <iostream> using namespace std;    void...
Analyze the following program and write down the output. # include <iostream> using namespace std;    void modifyArray( int [ ], int );    void modifyElement ( int );      int main( ) {   const int arraySize = 8;   int a[arraySize] = { 2, -2, 10, -3, -1 ,0, 10, -5 };      modifyArray ( a, arraySize);      for ( int i =0; i < arraySize; i++)               cout << a[i] << ‘  ’;         modifyElement ( a[4] );          for ( int i =0; i <...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT