Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

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:


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



About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!