+ 1
Write a sub procedure using a for next loop to extract all the vowls from the string
2 Answers
+ 5
What's a for next loop? Do you mean for_each()?
Function 1 - Without for_each, just plain for.
bool isvowel(char c)
{
return c=='a' || c=='e' || c=='i' || c=='o' \
|| c=='u' || c=='A' || c=='E' || c=='I' || \
c=='O' || c=='U' ;
}
string extract(string data)
{
string res("");
int len = data.length();
for(int i=0;i<len;i++)
{
if( isvowel(data.at(i) ) res += data.at(i);
}
return res;
}
Function 2 - Using for_each and iters. C++11.
string res(""); //Global
void vowel(char c)
{
if (c=='a' || c=='e' || c=='i' || c=='o' \
|| c=='u' || c=='A' || c=='E' || c=='I' || \
c=='O' || c=='U' ) res += c;
}
string extract(string data)
{
for_each(data.begin(),data.end(),vowel);
string dat(res);
return dat;
}