In: Computer Science
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 insert the following comments at the beginning of your program and write our commands in the middle:
Write a C++ Program:
/*
// Name: Your Name
// ID: Your ID
// Purpose Statement:~~~
*/
#include <iostream>
#include <string>
using namespace std;
void removeVowels(string& str);
bool isVowel(char ch);
int main()
{
string str;
cout << "Enter a string: ";
…
…
YOUR CODE HERE
…
…
return 0;
}
void removeVowels(string& str)
{
int len = str.length();
…
…
YOUR CODE HERE
…
…
}
bool isVowel(char ch)
{
switch (ch)
…
…
YOUR CODE HERE
…
…
}
Hi There, I have included the code snippet.Please let me know if you have any doubts.
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
string removeVowels(string str);
bool isVowel(char ch);
int main(){
string strr;
cout << "Enter a string: ";
cin>>strr;
cout<<removeVowels(strr)<<endl;
return 0;
}
// this function will remove vowels
string removeVowels(string str){
int len = str.length();
vector<char> vowels = {'a', 'e', 'i', 'o', 'u',
'A', 'E', 'I', 'O', 'U'};
for (int i = 0; i < len; i++)
{
if (find(vowels.begin(), vowels.end(),
str[i]) != vowels.end())
{
str = str.replace(i, 1, "");
i -= 2;
}
}
return str;
}
// this function will tell if a char is vowel or not.
bool isVowel(char ch){
if(ch=='a' || ch=='e' || ch=='i' ||
ch=='o' || ch=='u' || ch=='A' ||
ch=='E' || ch=='I' || ch=='O' ||
ch=='U'){
cout<<ch<<" is a vowel."<<endl;
}
else{
cout<<ch<<" is a consonant."<<endl;
}
return 0;
}