user2734182 user2734182 - 1 month ago 19
C# Question

PigLatin translate

I will create a program that translates English words into Pig Latin ... My problem with the code found below, is that the only word in the last index of the array as reported in the results? Does anyone see the error?

Thanks in advance

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnTrans_Click( object sender, EventArgs e )
{

string engWordText = engWord.Text.ToString();
string let1;
string restLet;
int position;
string pigLatin = "";
string vokal = "AEIOUaeiou";



// split the sentence into individual words
//string[] words = engWordText.Split(' ');
string[] transWord = engWordText.Split(' ');

// translate each word into pig latin
foreach (string word in transWord)
{
// check for empty TextBox
try
{
let1 = word.Substring(0, 1);
restLet = word.Substring(1, word.Length - 1);
position = vokal.IndexOf(let1);

if (position == -1)
{
pigLatin = restLet + let1 + "ay";
}
else
{
pigLatin = word + "way";
}

// display the translation
latinInput.Text = pigLatin.ToString();
engWord.Clear();

}
catch (System.ArgumentOutOfRangeException)
{
MessageBox.Show("Du måste skriva in ett engelskt ord", "PigLatin",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}


} // end method translateButton_Click

// pressing enter is the same as clicking the Translate Button
private void engWordText_KeyDown( object sender, KeyEventArgs e )
{
// allow user to press enter in TextBox
if ( e.KeyCode == Keys.Enter )
btnTrans_Click( sender, e );
} // end method inputTextBox_KeyDown


} // end class PigLatinForm

Answer

You are assigning the value of pigLatin to the text box's Text property at the end of each loop, which means it will only have the last value that was assigned to it. Try this:

 List<string> plWords = new List<string>();         
 // translate each word into pig latin
 foreach (string word in transWord)
 {
         // check for empty TextBox
         try
         {
             let1 = word[0];
             restLet = word.Substring(1, word.Length - 1);

             if (!vokal.Contains(let1))
             {
                 pigLatin = restLet + let1 + "ay";
             }
             else
             {
                 pigLatin = word + "way";
             }

             plWords.Add(pigLatin);
         }
         catch (System.ArgumentOutOfRangeException)
         {
             MessageBox.Show("Du måste skriva in ett engelskt ord", "PigLatin",
             MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
 }

 engWord.Clear();    
 latinInput.Text = string.Join(" ", plWords.ToArray());

As a bit of a bonus, here's how you can make this operation quite a bit cleaner using Linq:

private static string MakePigLatin(string word)
{
    const string vowels = "AEIOUaeiou";

    char let1 = word[0];
    string restLet = word.Substring(1, word.Length - 1);

    return vowels.Contains(let1) ? word + "way" : restLet + let1 + "ay";
}

private void btnTrans_Click( object sender, EventArgs e )
{
     var plWords = engWord.Text
                          .Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries)
                          .Select(MakePigLatin);

     latinInput.Text = string.Join(" ", plWords);
     engWord.Clear();    
}
Comments