So I have a Pig Latin Translator that supports multiple words. But whenever I enter some words (For this we'll just use "banana apple shears chocolate Theodore train" for example.) It will spit out the translated words correctly but it makes repeats! Here is my code:
namespace Pig_Latin_Translator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> vowels = new List<string>();
List<string> specials = new List<string>();
private void TranslateButton_Click(object sender, EventArgs e)
{
String[] parts = TranslateBox.Text.Split();
foreach (string s in specials)
{
if (TranslateBox.Text.Contains(s) || TranslateBox.Text == "\"")
{
TranslateOutput.Text = "";
MessageBox.Show("No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
break;
}
else
{
foreach (String part in parts)
{
foreach (String v in vowels)
{
if (part.Substring(0, 1) == v)
{
TranslateOutput.Text = TranslateOutput.Text + " " + part + "ay";
break;
}
else
{
if (part.Substring(0, 2) == "sh" || part.Substring(0, 2) == "ch" || part.Substring(0, 2) == "th" || part.Substring(0, 2) == "tr")
{
string SwitchP = part.Substring(2) + part.Substring(0, 2);
TranslateOutput.Text = TranslateOutput.Text + " " + SwitchP + "ay";
break;
}
else
{
string Switch = part.Substring(1) + part.Substring(0, 1);
TranslateOutput.Text = TranslateOutput.Text + " " + Switch + "ay";
break;
}
}
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
vowels.Add("a");
vowels.Add("e");
vowels.Add("i");
vowels.Add("o");
vowels.Add("u");
specials.Add("`");
specials.Add("1");
specials.Add("2");
specials.Add("3");
specials.Add("4");
specials.Add("5");
specials.Add("6");
specials.Add("7");
specials.Add("8");
specials.Add("9");
specials.Add("0");
specials.Add("-");
specials.Add("=");
specials.Add("[");
specials.Add("]");
specials.Add(@"\");
specials.Add(";");
specials.Add("'");
specials.Add(",");
specials.Add(".");
specials.Add("/");
specials.Add("~");
specials.Add("!");
specials.Add("@");
specials.Add("#");
specials.Add("$");
specials.Add("%");
specials.Add("^");
specials.Add("&");
specials.Add("*");
specials.Add("(");
specials.Add(")");
specials.Add("_");
specials.Add("+");
specials.Add("{");
specials.Add("}");
specials.Add("|");
specials.Add(":");
specials.Add("\"");
specials.Add("<");
specials.Add(">");
specials.Add("?");
}
private void AboutButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Pig Latin is a fake language. It works by taking the first letter (Or two if it's a pair like 'th' or 'ch') and bringing it to the end, unless the first letter is a vowel. Then add 'ay' to the end. So 'bus' becomes 'usbay', 'thank' becomes 'ankthay' and 'apple' becomes 'appleay'.", "About:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
Which outputs if you typed in "banana apple shears chocolate Theodore train":
"ananabay appleay earsshay ocolatechay heodoreTay aintray" repeating over 10 times.
BTW: Sorry if you can't answer because I know there is LOTS of code. But it doesn't matter because the thing still is useful. It's just that it shouldn't happen and get on my nerves. And I know there is still many glitches and MUCH more to do but I want to get this resolved first.
You are nesting your code in two loops that it shouldn't be nested in
foreach (string s in specials)
and
foreach (String v in vowels)
Your break
statements are getting you out of trouble for one, but not the other.
You can avoid these loops entirely is you use the .Any(...)
predicate.
Here's what your code could look like:
private void TranslateButton_Click(object sender, EventArgs e)
{
TranslateOutput.Text = "";
if (specials.Any(s => TranslateBox.Text.Contains(s)))
{
MessageBox.Show("No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
String[] parts = TranslateBox.Text.Split();
foreach (var part in parts)
{
var index = 1;
if (vowels.Any(v => part.Substring(0, 1).ToLower() == v))
{
index = 0;
}
else if (new [] { "sh", "ch", "th", "tr", }.Contains(part.Substring(0, 2).ToLower()))
{
index = 2;
}
TranslateOutput.Text += " " + part.Substring(index) + part.Substring(0, index);
}
}
TranslateOutput.Text = TranslateOutput.Text.TrimEnd();
}
This brings it down to the one foreach
loop that you actually need.
You can also get your code to initalize vowels
and specials
down to this:
vowels.AddRange("aeiou".Select(x => x.ToString()));
specials.AddRange(@"`1234567890-=[]\;',./~!@#$%^&*()_+{}|:""<>?".Select(x => x.ToString()));