After eraser() function, you should issue i-- and reduce number for string lengt
ID: 663601 • Letter: A
Question
After eraser() function, you should issue i-- and reduce number for string length, too I need to do these to things thanks in advance.
can someone please help me here is my program
#include <iostream>
#include <string>
using namespace std;
bool Vowel(char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
return true;
else
return false;
}
void Remove(string & string1, int index)
{
string1.erase(index, 1);
}
int main()
{
cout << "Enter a string or press the enter key to quit" << " " << ": ";
string string1;
while (getline(cin, string1) && string1.size() > 0)
{
string::size_type original = string1.size();
for (int i = 0; i < string1.size(); i++)
{
if (Vowel(string1[i]))
Remove(string1, i);
}
string::size_type altered = string1.size();
if (original != altered)
cout << "String with no vowels = "<< string1 << endl;
else
cout << "No vowels in string present ";
cout << " Enter another string or press the enter key to quit" << " " << ": ";
}
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
bool Vowel(char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
return true;
else
return false;
}
void Remove(string & string1, int index)
{
string1.erase(index, 1);
}
int main()
{
cout << "Enter a string or press the enter key to quit" << " " << ": ";
string string1;
while (getline(cin, string1) && string1.size() > 0)
{
string::size_type original = string1.size();
for (int i = 0; i < string1.size(); i++)
{
if (Vowel(string1[i]))
{
Remove(string1, i);
i--;
}
}
string::size_type altered = string1.size();
if (original != altered)
{
cout << "String with no vowels = "<< string1 << endl;
}
else
cout << "No vowels in string present ";
cout << " Enter another string or press the enter key to quit" << " " << ": ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.