The goal of my assignment is simple:
Write a program that converts a given text to "Pig Latin". Pig Latin consists of removing the first letter of each word in a sentence and placing that letter at the end of the word. This is followed by appending the word with letters "ay".
Example
Input: THIS IS A TEST Output: HISTAY SIAY AAY ESTTAY
I want to know if there is any way to write this part of the code in a different/better way:
foreach (string word in engword.Split())
Here is my full code:
string engword = textBox1.Text; //english word
string pig1 = ""; //pig latin
string pig2 = ""; //first letter
string space = " ";
string extra = ""; //extra letters
int pos = 0; //position
foreach (string word in engword.Split())
{
if (pos != 0)
{
pig1 = pig1 + space;
}
else
{
pos = 1;
}
pig2 = word.Substring(0,1);
extra = word.Substring(1, word.Length - 1);
pig1 = pig1 + extra + pig2 + "ay";
}
MessageBox.Show(pig1.ToString());
}
}
}
string.Empty
is generally preferred to the empty string literal,""
. Also ifpos
only ever equals1
or0
, why not use abool
? \$\endgroup\$