I'm reading a book for C++ and one of the exercises to create a pig latin translator. I have figured out all the necessary steps to translate a single word. Now I am having a lot of trouble with making a function for handling multi-word strings.
Basically I need help with the sort of standard idiom for iterating through each word of a string and performing an action on each word.
The function I have so far is sloppy at best and I am just stuck.
string sentenceToPigLatin(string str) {
string result = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.at(i);
if (ch == ' ') {
result += toPigLatin(str.substr(0, i));
str = str.substr(i);
}
}
return result;
}
You can assume toPigLatin() performs the correct procedure for a word not containing whitespace.