My Submission (Click to View)
![[Image: pig.png]](http://www.coryshivers.com/upload/files/pig.png)
Click here to download the Binary
Click here to download demo projects (C# and VB.Net)
VB.Net (fixed a bug to check whether the word has a vowel)
Code:
Public Class Form1
'.Net Coding Challeng #1 by KoBE.
'Enjoy!
Private Sub btnEngtoPig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEngtoPig.Click
'Checks to make sure text is entered
If txtConvert.Text = "" Then
MessageBox.Show("No text was entered")
Exit Sub
End If
'splits the entire sentence into words
Dim strWords() As String = txtConvert.Text.Split(" ")
For i = 0 To strWords.Length - 1
'this loops through each word converting it to pig-latin
strWords(i) = EngToPig(strWords(i))
Next
'after each word is converted, it joins the string together and seperates
'each word by a space
txtOutput.Text = Join(strWords, " ")
End Sub
Private Sub btnPigtoEng_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPigtoEng.Click
'if there are no hyphens, then we can't translate
If Not txtConvert.Text.Contains("-") Then
MessageBox.Show("This is not a valid Pig-Latin senence")
Exit Sub
End If
'sepearte each word into a string array
Dim strWords() As String = txtConvert.Text.Split(" ")
For i = 0 To strWords.Length - 1
'translate each word
strWords(i) = PigToEng(strWords(i))
Next
'join each translated word back to a sentence
txtOutput.Text = Join(strWords, " ")
End Sub
Private Function EngToPig(ByVal strWord As String) As String
Dim chrVowel As Char 'used to find our first vowel
Dim strSplit As String 'used to take the first part of the word and append at the end
Dim strResult As String 'holds our finished value
Dim iIndex As Integer 'helps split the text at the vowel
For i = 0 To strWord.Length - 1
'this loops through each character to find the first vowel
chrVowel = strWord.Substring(i, 1)
Select Case chrVowel.ToString.ToLower
Case "a", "e", "i", "o", "u", "y"
'if it's the first character only append -way to the end
If i = 0 Then Return strWord & "-way"
'strSplit becomes what we attach to the end
strSplit = strWord.Substring(0, i).ToLower
'this is going to be the position of the first vowel
iIndex = strWord.IndexOf(chrVowel)
'we want our new word to start at the vowel, then have everthing else added to the end with 'ay'
strResult = strWord.Substring(iIndex, strWord.Length - iIndex) & "-" & strSplit & "ay"
'return the translated word
Return strResult
Exit For
End Select
Next
MessageBox.Show("There are no vowels. Therefore a conversion cannot be done")
Return ""
End Function
Private Function PigToEng(ByVal strWord As String) As String
Dim iIndex As Integer = strWord.IndexOf("-") 'used to find our split
Dim iLastIndex As Integer = strWord.IndexOf("ay") 'used with iIndex to find the characters between "-" and "ay"
Dim strResult As String 'used to hold the finished product
If strWord.Substring(iIndex + 1, 1).ToLower = "w" Then
'if its a w then just remove '-way'
strResult = strWord.Remove(iIndex)
Else
'takes the letters between '-' and 'ay' and adds it to the front then removes the junk
strResult = strWord.Substring(iIndex + 1, iLastIndex - iIndex - 1) & strWord.Substring(0, iIndex)
End If
Return strResult
End Function
End Class
C#
Code:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnEngtoPig_Click(object sender, EventArgs e)
{
//Checks to make sure text is entered
if (txtConvert.Text == null)
{
MessageBox.Show("No text was entered");
return;
}
//splits the entire sentence into words
string[] strWords = txtConvert.Text.Split(' ');
for (int i = 0; i < strWords.Length; i++)
{
//this loops through each word converting it to pig-latin
strWords[i] = EngToPig(strWords[i]);
}
//after each word is converted, it joins the string together and seperates
//each word by a space
txtOutput.Text = String.Join(" ", strWords);
}//end btnEngtoPig
private void btnPigtoEng_Click(object sender, EventArgs e)
{
//if there are no hyphens, then we can't translate
if (!txtConvert.Text.Contains("-"))
{
MessageBox.Show("This is not a valid Pig-Latin senence");
return;
}
//sepearte each word into a string array
string[] strWords = txtConvert.Text.Split(' ');
for (int i = 0; i < strWords.Length; i++)
{
//translate each word
strWords[i] = PigToEng(strWords[i]);
}
//join each translated word back to a sentence
txtOutput.Text = String.Join(" ", strWords);
}//end btPigtoEng
private string EngToPig(string strWord)
{
string chrVowel = null; //used to find our first vowel
string strSplit = null; //used to take the first part of the word and append at the end
string strResult; //holds our finished value
int iIndex = 0; //helps split the text at the vowel
for (int i = 0; i < strWord.Length; i++)
{
//this loops through each character to find the first vowel
chrVowel = strWord.Substring(i, 1);
switch (chrVowel.ToLower())
{
case "a":
case "e":
case "i":
case "o":
case "u":
case "y":
//if it's the first character only append -way to the end
if (i == 0)
return strWord + "-way";
//strSplit becomes what we attach to the end
strSplit = strWord.Substring(0, i).ToLower();
//this is going to be the position of the first vowel
iIndex = strWord.IndexOf(chrVowel);
//we want our new word to start at the vowel, then have everthing else added to the end with 'ay'
strResult = strWord.Substring(iIndex, strWord.Length - iIndex) + "-" + strSplit + "ay";
//return the translated word
return strResult;
}//end switch
}
MessageBox.Show("There are no vowels. Therefore a conversion cannot be done");
return "";
}//end function
private string PigToEng(string strWord)
{
int iIndex = strWord.IndexOf("-"); //used to find our split
int iLastIndex = strWord.IndexOf("ay"); //used with iIndex to find the characters between "-" and "ay"
string strResult = null; //used to hold the finished product
if (strWord.Substring(iIndex + 1, 1).ToLower() == "w")
{
//if its a w then just remove '-way'
strResult = strWord.Remove(iIndex);
}
else
{
//takes the letters between '-' and 'ay' and adds it to the front then removes the junk
strResult = strWord.Substring(iIndex + 1, iLastIndex - iIndex - 1) + strWord.Substring(0, iIndex);
}
return strResult;
}//end function
}//end form1
}
Wasn't as bad as I though it would be. I didnt try to make a GUI at all so don't hate on it. I simply did the program. It uses two functions. One to convert to Pig-Latin and another to convert to English.
Edit: Did it in C# as well for the heck of it.
(04-16-2011, 11:29 AM)The High Roller Wrote: I am good at VB, I just never focused on arrays. Promise you that, you know when pros can do such hard things and cant do the easy? Yes thats me.
You should brush up on arrays then. They are actually quite handy. Also, you didn't use any functions in yours. Thought I'd point that out. Your submission wasn't too bad though. Good job.
In yours, look at how it translates: chinese
(04-16-2011, 03:03 AM)Untouch Wrote: Seems pretty easy to do, well in java anyway.
Give it a shot
![Pirate Pirate](https://www.supportforums.net/images/smilies/pirate.gif)
I code at http://tech.reboot.pro