0

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'));
}
Megan
  • 119
  • 1
  • 3
  • 13
  • It looks like it is working, but you're not advancing past the spaces - so instead of giving the function "this" and getting "histhay" back, you're giving it " this" so are getting "this ay" back. (hint: Trim the whitespace on the string before passing it into the function) – benjymous Nov 20 '13 at 10:52
  • Also, unrelated to the issue you're seeing, there's a bug in your `isVowel()` function (what's the last vowel in the alphabet?) – benjymous Nov 20 '13 at 10:53
  • I'm reminded of this http://www.youtube.com/watch?v=IIAdHEwiAy8 :-) – vogomatix Nov 20 '13 at 10:55
  • Try changing sentence = sentence.erase(0,i); to sentence = sentence.erase(0,i+1); It seems that you are not deleting the spaces for the next for loop iteration. – Abhishek Bansal Nov 20 '13 at 11:07
  • @benjymous The professor stated we should treat 'y' as a consonant. If a word starts with y it usually is treated like one anyway - word final y's are more like vowels. – Megan Nov 20 '13 at 13:14
  • @Megan, Look at your code again - What have you got against the letter 'u'? :-) – benjymous Nov 20 '13 at 13:16

3 Answers3

1

Two errors:

for(int ...; ; i = sentence.find(" ",i)) { // next space find should start from 0.
        //..
        //..
        sentence = sentence.erase(0,i);// you should delete the current space found also.
        i++;
    }

Change this to:

for(int ...; ; i = sentence.find(" ",0)) { 
        //..
        //..
        sentence = sentence.erase(0,i+1);
        i++;
    }

Output:

esttay histay tringsay
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
0

The code is confused because you are trying to skip the word you have already found and you are trying to remove the word you have already found from sentence. Do one or the other.

john
  • 85,011
  • 4
  • 57
  • 81
0

Can I recommend that you use the POSIX regular expression library or PCRE, which should make swapping the first and last letters of words simple.

A regex such as \b(\w)(\w+)(\w)\b could be replaced by swapping the first and last collection groups

vogomatix
  • 4,856
  • 2
  • 23
  • 46