112713 Could you please look at the code? Is it correct? What are the pre-condit
ID: 3547372 • Letter: 1
Question
112713
Could you please look at the code? Is it correct? What are the pre-condition and post-condition for all functions which are as follows:
Q: Write a program that prompts the user to input a string. Theprogram then uses the function substr to removeall the vowels from the string. For example, if str ="There", thenafter removing all the vowels, str="Thr". After removing all thevowels, output the string. Your program must contain a functionremove all the vowels and a function to determine whether acharacter is a vowel.
#include <iostream>
#include <string>
using namespace std;
string removevowels (string&);
bool isvowel (char);
int main()
{
string word;
cout << "Enter a string" << endl;
getline (cin,word);
cout<<"with vowels: "<<word<<endl;
word=removevowels(word);
cout<<"without vowels: "<<word<<endl;
char any_keystroke;
cin.ignore(200, ' ');
cout << " ";
cout << "Program execution has ended normally. ";
cout << "Press the enter key to exit.";
cin.get(any_keystroke);
return 0;
}
string removevowels(string& word )
{int i,prev=0;
string result="";
for(i=0;word[i]!='';i++)
if(isvowel(word[i]))
{result=result+word.substr(prev,i-prev);
prev=i+1;
}
result=result+word.substr(prev,i-prev);
return result;
}
bool isvowel(char in)
{bool value=false;
switch(in)
{ case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': value= true;
}
return value;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
string removevowels(string&);
bool isvowel(char);
int main()
{
string word;
cout << "Enter a string" << endl;
getline (cin,word);
cout<<"with vowels: "<<word<<endl;
word=removevowels(word);
cout<<"without vowels: "<<word<<endl;
char any_keystroke;//reads and ignores up to 200 characters, or ' ' is read and ignored
cin.ignore(200, ' ');
cout << " ";
cout << "Program execution has ended normally. ";
cout << "Press the enter key to exit.";
cin.get(any_keystroke);
return 0;
}
string removevowels(string& word )
{int i,prev=0;
string result="";
for(i=0;word[i]!='';i++)
if(isvowel(word[i]))
{result=result+word.substr(prev,i-prev);
prev=i+1;
}
result=result+word.substr(prev,i-prev);
return result;
}
bool isvowel(char in)
{bool value=false;
switch(in)
{ case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': value= true;
}
return value;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.