Hi, I am trying to get ahead of my course work for next term and teaching myself c#. I am trying to write a pig latin translator as an exercise from the book I am using but it has no answers. I believe I need to use the split and trim methods and maybe the begins with and ends with methods of the string?
I just dont know how to tie it all together and can't seem to find a working answer online!
Any help would be appreciated:

 string english = Convert.ToString(txtEnglish.Text);


                string pig = "";
                string first;
                string last;
                string vowels = "AEIOUaeiou";
                int consonant;

                string[] words = txtEnglish.Text.Split(' ');

                foreach (string word in words)
                {
                    try
                    {
                        first = word.Substring(0, 1);
                        last = word.Substring(1, word.Length - 1);
                        consonant = vowels.IndexOf(first);
                        if (consonant == -1)
                        {
                            pig = last + first + "ay";
                        }

                        else
                        {
                            pig = word + "way";
                        }
                    }


                    //   txtPig.Text = txtPig.ToString();

                }
            }

        }

It looks like all you need the variable to hold the translated string as it is built up.
Define another string at the top and each loop through concatenate the translated pig onto it.
You can use the StringBuilder class for this rather than doing += on a string for a performance gain.

Remove the first line.
txtEnglish.Text is already a string you don't have to convert it.
This is a little console application i came up with using your modified code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            const string SPACE = " ";
                string pig = "";
                string first;
                string last;

                string txtEnglish = "This is a piglatin generator";
                string[] words = txtEnglish.Split(' ');

                foreach (string word in words)
                {
                    first = word.Substring(0, 1);                        
                    last = word.Substring(1, word.Length - 1);                      
                    bool isVowel = "aeiouAEIOU".IndexOf(first) >= 0;
                    if (isVowel)
                    {
                        pig += word + "way" + SPACE;                       
                    }
                    else
                    {
                        pig += last + first + "ay" + SPACE;
                    }
                }
                Console.WriteLine(pig);
                Console.ReadKey();
            }
        } 
}

Hope it helps. You should also consider punctuation marks and proper capitalisation.

4cfbf37ae2fedd3436cb9a1112484e17 thank you so much ddanbe...I am trying to use your code to create it with a gui so when you click the translate button it translates the english from one text box to another, so do I have to change the way the split is done to get that to work? I keep getting errors!!

I was able to reproduce your error if I placed a blank at the end of
my string to translate. This causes Split to put an empty string in the
words array.
I replaced line 19 of my code with string[] words = txtEnglish.Trim().Split(' '); and all went well.
Substring is not very happy if it has to cut something out of an empty string. :)
Perhaps it is still something else, but you could give this a try.

There are a number of things your not considering.

  1. Pig Latin doesn't just transpose the first consonant, it transposes the first consonant sound. For instance story becomes orystay not torysay
  2. If the first letter is capitalized the new first letter should be capitalized as well
  3. If the word has punctuation on the end that should be moved to the end of the new word.

Here's a function that should help:

string WordToPigLatin(string input)
{
    const string vowels = "aeiouAEIOU";
    int index = 1;
    int length = input.Length;
    char first = input[0];
    if(vowels.Contains(first))
        return input + "way";
    input = input.ToLower();
    for (; index < length; index++)
    {
        if (vowels.Contains(input[index]))
        {
            break;
        }
    }
    string firstpart = "";
    string lastpart = input.Substring(0, index) + "ay";
    if(char.IsUpper(first))
        firstpart = char.ToUpper(input[index++]) + input.Substring(index,length - index);
    else
        firstpart = input.Substring(index,length - index);
    return  firstpart + lastpart ;
}

Since this code only deals with individual words here's a way to call it and fill a textbox:

        foreach(string s in txtEnglish.Text.Split(" ".ToArray(), _ StringSplitOptions.RemoveEmptyEntries))
        {
            if (char.IsPunctuation(s.Last()))
                txtPig.Text += WordToPigLatin(s.Substring(0, s.Length - 1)) + s.Last() + " ";
            else
                txtPig.Text += WordToPigLatin(s) + " ";
        }

I left handling the punctuation here, as it seemed to be more efficient this way.

The main thing not handled here is hyphenated words, which should only very rarely be an issue, as I can't think of any that don't start with at leat one syllable.

ok...so now I am really confused..I am having shall we say to many errors to mention happening now! Please bear in mind this is only my second week of trying to learn it..this is the exercise I am trying to do...Anyone have a simple dumbed down version??

Pretty much all of the points I mentioned are in your assignment. The function and the calling code I showed you are written to cover those points. Where is the confusion and what errors are resulting from that code?

the errors occur with the last piece of code you entered?
where should I be placing that in my code??

I found the error!
Thank you very much!! :)

You're welcome. In looking over the assignment, I noticed one rule that's not in the code, handling Y's.

string WordToPigLatin(string input)
{
    const string vowels = "aeiouAEIOU";
    int index = 1;
    int length = input.Length;
    char first = input[0];
    if(vowels.Contains(first))
    return input + "way";
    input = input.ToLower();
    for (; index < length; index++)
    {
        if (vowels.Contains(input[index]) || input[index] == 'y')
        {
            break;
        }
    }
    string firstpart = "";
    string lastpart = input.Substring(0, index) + "ay";
    if(char.IsUpper(first))
    firstpart = char.ToUpper(input[index++]) + input.Substring(index,length - index);
    else
    firstpart = input.Substring(index,length - index);
    return firstpart + lastpart ;
}

Just the one slight modification to check for 'y' since it is a special case.

If this completes your answer please remember to mark this solved. Thanks.

Hey, so I have been running this code over the weekend and trying to work on it, I am still running into some problems, if I type in say CASE it translates to Asecay instead of ASECAY, and when i type in numbers/symbols etc it translates 123 to 123ay instead of just 123?
What am I doing wrong....
me and c# are not friends atm haha

Thanks in advance..

remove this line, input = input.ToLower();, and it should work better.

Yeah I tried that but I get a formatting error then in my text when I say type This it translates to
Is Thay...

Huyay eakspay atinlay? It's been awhile... :-)

I got the last two words...as for the first word...I think my brain has officialy given up on pig latin haha.

Translation - "Who speaks latin?". :-) My sister and I would speak pig latin around our parents when we were kids and didn't want them to understand us. This was back in the 1950's - hence my comment "It's been awhile." :lol:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.