I am writing a pig latin converter that takes a string as a command line argument and converts each word in the sentence into pig latin. I am trying to break up the string into individual words by finding spaces, but the output I get with the below code... honestly makes no sense.
It works for the first word, but then doesn't convert any subsequent words. So "test this string" becomes "esttay this ay string ay."
At this point we haven't covered vectors, so the solution has to be relatively simple.
Any ideas?
#include <iostream>
#include <string>
using namespace std;
void convertSentence(string sentence);
string pigLatinWord(string input);
bool isVowel(string input);
int main(int argc, char* argv[]) {
if(argc != 2) {
cout << "USAGE: " << argv[0] << " \"[sentence]\"" << endl;
} else {
string sentence(argv[1]);
convertSentence(sentence);
}
return 0;
}
void convertSentence(string sentence) {
string word = "", remainder = sentence, pigLatin = "";
for(int i = sentence.find(" ",0); i != string::npos; i = sentence.find(" ",i)) {
word = sentence.substr(0,i);
pigLatin += pigLatinWord(word) + " ";
sentence = sentence.erase(0,i);
i++;
}
pigLatin += pigLatinWord(sentence);
cout << pigLatin << endl;
}
string pigLatinWord(string input) {
string word = "";
// If the first letter is a vowel, simply add "yay" to the end of it.
if (isVowel(input)) {
word = input + "yay";
//If the first letter is a consonant, add the first letter to the end,
//delete the first letter, then add "ay." - Credit to Tyler Sorrels
//CString/String solution post on Piazza. I had issues working with
//substrings, and this ended up being easier to read.
//But I did add a check for words that start with two consonants
//to cover all cases.
} else {
input += input.at(0);
input = input.erase(0,1);
word = input + "ay";
}
return word;
}
// Checks if the first letter of the word is a vowel.
// Returns true if yes, false if no.
bool isVowel(string input) {
return ((input[0] == 'a') || (input[0] == 'e') || (input[0] == 'i') || (input[0] == 'o') || (input[0] == 'a'));
}