st0ne
st0ne

Reputation: 11

Pig Latin Console

Hi I'm doing Pig Latin for class, the instructions were first consonant is removed from the front of the word, and put on the back of the word. Then followed by the letters "ay." examples are, book becomes ookbay, and strength becomes engthstray. I'm having trouble because it doesn't do the first consonant.

// button, three, nix, eagle, and troubadour
Console.Write("Enter word you want in Pig Latin: ");
string word1 = Console.ReadLine(); 
string pig = ""; 
string vowels = "aeiouAEIOU"; 
string space = " ";
string extra = ""; //extra letters
int pos = 0; //position

foreach (string word in word1.Split())
{
    if (pos != 0)
    {
        pig = pig + space;
    }
    else
    {
        pos = 1;
    }

    vowels = word.Substring(0,1);
    extra = word.Substring(1, word.Length - 1);
    pig = pig + extra + vowels + "ay";
}

Console.WriteLine(pig.ToString());

For example if I do strength it will come up as trengthsay and not like the example

Upvotes: 1

Views: 534

Answers (1)

Ian
Ian

Reputation: 1504

You've got a number of problems there. First of all, your definition of the problem:

the instructions were first consonant is removed from the front of the word

That is precisely what you have done. strength does become trengths if you move the first consonant. You need to change your definition to all leading consonants up to the first vowel. Also, what do you do in the case of eagle? Does it become eagleay? Your instructions don't specify how to deal with a leading vowel.

This is another problem

vowels = word.Substring(0,1);  // This will overwrite your vowel array with the first letter

Don't worry about writing real code just yet, write some pseudo-code to work out your logic first. @Chris's comment about looking for the first vowel is a good one. Your pseudo code may look something like:

Check if word begins with consonant
{
    If so, look for index of first vowel
    Take substring of word up to first vowel.
    Append it to end
}
Otherwise
{
    Deal with leading vowel
}

Upvotes: 3

Related Questions