I have to create a program that will count the number of vowels and consonants in a string
I need to create 2 functions:
1. a value returning function hat accepts a string as an argument, and returns the number of vowels (including y)
2. a function that accepts a string as an argument, and returns the number of consonants
get string
pass string to function which counts vowels
return number of vowels
pass string to function which counts consonants
return number of consonants
display number of vowels
display number of consonants
end
/********************************************************************************
** Author : Donald "Chris" Moore
** Date : 2 April 2015
** File : Assign-7-Lab-final.cpp
** Assignment : Write a program that prompts the user to input a string.
** : The program then uses the function substr to remove all of
** : the vowels from the string. For example, if str = “There”,
** : then after removing all of the vowels, str = “Thr”. After
** : removing all of the vowels, output the string. Your program
** : must contain a function to remove all of the vowels and a
** : function to determine whether a character is a vowel.
** Input from : Console
** Output to : Console
********************************************************************************/
#include <iostream> // is required for the use of cin and cout
#include <string> // is required for the use of string type data
usingnamespace std;
// Function Prototypes
bool isVowel(char ch);
void removeVowel(string & toTest);
void toContinue();
int main()
{
string userInput;
cout << "Please enter a string for vowel extraction: ";
getline(cin, userInput);
removeVowel(userInput);
cout << "The new string is: ";
cout << endl << userInput << endl;
toContinue();
return 0;
} // End of Main
//******************************************************************************
// the beginning of 'removeVowel' function called from Main
//******************************************************************************
void removeVowel(string & toTest)
{
string a; // temporary part of string before vowel
string b; // temporary part of string after vowel
int len = 0; // Length of the original string
int i = 0; // loop indicees
len = toTest.length();
for(i=len-1 ; i >= 0 ; --i )
{
if (isVowel(toTest[i]))
{
a = toTest.substr(0,i);
b = toTest.substr(i+1);
toTest = a + b;
}
}
} // End of removeVowel
//******************************************************************************
// the beginning of 'isVowel' function called from removeVowel
//******************************************************************************
bool isVowel(char ch)
{
switch(toupper(ch))
{
case'A':
case'E':
case'I':
case'O':
case'U':
returntrue;
default:
returnfalse;
}
} // End of isVowel
//******************************************************************************
// the beginning of 'toContinue' function
//******************************************************************************
void toContinue()
{
cout << endl << "Press the \'Enter\' key to continue" << endl;
cin.ignore();
} // end toContinue
Hey steph2015, here is my version to your question. All within the main function, though. You can break down into functions. That should be pretty simple ...