You have three important indices for each word, namely
- The starting letter, let's name it
start
. - The index of the vowel, let's name it
vowel
- The index of the blank (or of the pass-the-end of the string), let's call it
end
Once you realize that the implementation is straightforward:
void piglatin(const string & sSentence)
{
size_t start, vowel, end;
start = 0;
do
{
end = sSentence.find(' ', start);
vowel = sSentence.find_first_of("aeiouAEIOU", start);
cout << sSentence.substr(vowel, end-vowel) << sSentence.substr(start, vowel-start) << "ay ";
start = end + 1;
} while (end != string::npos);
cout << endl;
}