C# translator from Pig Latin to English
Obviously I'm new to this, hence the content of this project. I have written some code that will translate English into Pig Latin. Easy enough. The problem is I want to find a way to translate the Pig Latin back into English using a logic block. The clone string just seems like a cheap way to do it. Any suggestions? Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FunctionTest
{
public class PigLatinClass
{
public static void pigTalk(string sentence)
{
try
{
while (sentence != "exit")
{
string firstLetter;
string afterFirst;
string pigLatinOut = "";
int x;
string vowel = "AEIOUaeiou";
Console.WriteLine("Enter a sentence to convert into PigLatin");
sentence = Console.ReadLine();
string[] pieces = sentence.Split();
foreach (string piece in pieces)
{
afterFirst = piece.Substring(1);
firstLetter = piece.Substring(0, 1);
x = vowel.IndexOf(firstLetter);
if (x == -1)
{
pigLatinOut = (afterFirst + firstLetter + "ay ");
}
else
{
pigLatinOut = (firstLetter + afterFirst + "way ");
}
Console.Write(pigLatinOut);
}
Console.WriteLine("Press Enter to flip the sentence back.");
Console.ReadKey(true);
string clonedString = null;
clonedString = (String)sentence.Clone();
Console.WriteLine(clonedString);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
The problem is there's no real rule that would work. For example: If the 3rd letter from the last was "w" you m开发者_开发技巧ight want to say this is a vowel word but, a consonant word starting with a "w" could also fit this rule. If the first letter was a vowel again you might want to say that this is a vowel word but, a consonant word could also fit this rule since the first letter is moved to the back (pat = atpay). The only way I think this is possible is to have an if statement that checks if w is in the 3rd position and the word starts with a vowel which would call for && operator and Visual Studio yells at you if you use it with strings.
The problem is that Pig Latin/English translation is not a bijective function.
For example, imagine to have 2 English words like "all"
and "wall"
, the corresponding Pig Latin words will be always "allway"
.
This suggest you that if you get a word like "allway"
you can't give a unique translation in English, but (at least) two.
I'm assuming this is homework.
What your professor probably wants is for you to convert a sentence to pig latin, and from pig latin. Keeping a copy of the original string only lets you "flip back" from sentences you already know the non-pig latin version of. It doesn't allow you to flip back from any string.
I think you want to structure your program like this:
public class PigLatinClass
{
public static string ToPigLatin(string sentence)
{
// Convert a string to pig latin
}
public static string FromPigLatin(string sentence)
{
// Convert a string from pig latin (opposite logic of above)
}
public static string PigTalk()
{
string sentence;
Console.WriteLine("Enter a sentence to convert into PigLatin");
sentence = Console.ReadLine();
sentence = ToPigLatin(sentence);
Console.WriteLine(sentence);
Console.WriteLine("Press Enter to flip the sentence back.");
Console.ReadKey(true);
sentence = FromPigLatin(sentence);
Console.WriteLine(sentence);
}
}
精彩评论