In: Computer Science
Write a C++ Program
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
*/
#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
…
…
}
Code:
#include <iostream>
#include <string>
using namespace std;
void removeVowels(string& str);
bool isVowel(char ch);
int main()
{
string str;
cout << "Enter a string: ";
getline(cin,str);
removeVowels(str);
cout<<"string without vowels is
"<<str;
return 0;
}
void removeVowels(string &str)
{
int len = str.length();
int i=0;
while(i<len){
if(isVowel(str[i])){
str=str.substr(0,i)+str.substr(i+1,len-i+1);
len=str.length();
}
else{
i=i+1;
}
}
}
bool isVowel(char ch)
{
switch (ch){
case 'a':
case 'A':
return 1;
break;
case 'e':
case 'E':
return 1;
break;
case 'i':
case 'I':
return 1;
break;
case 'o':
case 'O':
return 1;
break;
case 'u':
case 'U':
return 1;
break;
default :
return 0;
}
}
Output: