result += test.substr(i, 1);
replace the second i with a 1, 1 is the number of chars, or length you want returned
Question
I'm new to c++... I'm trying to make a function that takes in a string and returns only the vowels in the string. I need to use substrings to perform this. If I use a word with only 1 vowel the function runs correctly... however I've tested a few other strings and it does not work. Do any of you have some suggestions? Thanks so much!
#include <iostream>
using namespace std;
string vowel_search(string str)
{
string test = str;
string result = "";
for (int i = 0; i < test.length(); i++)
{
if (str[i] == 'a' || str[i] == 'A'||
str[i] == 'e' || str[i] == 'E' ||
str[i]== 'i'|| str[i] == 'I' ||
str[i] == 'o' || str[i] == 'O' ||
str[i] == 'u' || str[i] == 'U')
{
result += test.substr(i, i);
}
}
return result;
}
int main() {
string strWord = "Aaron";
string str1 = vowel_search(strWord);
cout << str1 << endl; // prints out: aon
string strWord1 = "Harry";
string str2 = vowel_search(strWord1);
cout << str2 << endl; //prints out: a -- Correct
string strSentence = "Harry is Funny";
string str3 = vowel_search(strSentence);
cout << str3 << endl; //prints out: ais Fununny
return 0;
}
Solution
result += test.substr(i, 1);
replace the second i with a 1, 1 is the number of chars, or length you want returned