I need a program that will remove all vowels from a string. I am kind of close t
ID: 3633335 • Letter: I
Question
I need a program that will remove all vowels from a string. I am kind of close to the result I think but I need someone to expand on what I have here without any additional headers and trying to mostly keep what's already here, just change around and add what needs to be added to get the correct result. I cannot figure out what I'm doing wrong. Also, in solving it, please provide explanation in the form of comments as to why you did what you did.//Program will remove all vowels from a user inputted string and output the string sans vowels.
#include <iostream> //Header file declaration.
#include <string>
using namespace std;
bool vowelDef(int vowel); //Prototype for my function.
void removeVowel(string& str);
int main() //Main function, will be executed first.
{
string string1, string2;
cout << "Enter a string: ";
removeVowel(string2);
cout << "After removing vowels: " << string2 << endl;
system ("pause");
return 0;
}
void removeVowel(string& str)
{
for (int vowel = 0; vowel < str.length(); vowel++)
{
if (vowelDef(str.erase[vowel, 1]));
}
}
bool vowelDef(int vowel)
{
string string1;
cin >> string1;
cout << "Before removing vowels: " << string1 << endl;
if (string1[vowel] =='A' || 'a' || 'E' || 'e'|| 'I' || 'i'|| 'O' || 'o' || 'U' || 'u')
string1.erase(string1[vowel], 1);
}
Explanation / Answer
#include <iostream> //Header file declaration.
#include <string>
using namespace std;
//prototype for vowel definition function
bool vowelDef(char vowel);
//Prototype for removing vowel character from given string
void removeVowel(string& str);
int main() //Main function, will be executed first.
{
string string1;
cout << "Enter a string: ";
cin >> string1;//Reading user input string
cout << "Before removing vowels: " << string1 << endl;//Printing Input
removeVowel(string1);//Calling removeVowel function
cout << "After removing vowels: " << string1 << endl;//Printing Output
system ("pause");
return 0;
}
//This is the function for removing the vowels from given string.
//string& str --> indicates pass by reference. it means, the original
//variable will be modified, no need to return new string
void removeVowel(string& str)
{
string newString="";//Temporary string for saving the result
//Loop through all the character in the given string and check whether
//is a vowel or not. If it is not vowel then add to new string
for (int vowel = 0; vowel < str.length(); vowel++)
{
//Condition for checking the given character is vowel or not
if (!vowelDef(str[vowel]))
{
//If given character is not a vowel, then add to output
newString = newString + str[vowel];
}
}
str = newString;//Assigning it back to original string
}
//Function for checking given character is vowel or not
bool vowelDef(char vowel)
{
//if the input character is not a,A,e,E,i,I,o,O,u,U then it will return false else return true
if (vowel =='A' || vowel =='a' || vowel =='E' || vowel =='e'|| vowel =='I' || vowel =='i'|| vowel =='O' || vowel =='o' || vowel =='U' || vowel =='u')
return true;
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.