2016-04-27 6 views
0

Ich habe Probleme beim Erstellen einer Anwendung in C# Konvertieren von Englisch in Schwein Latein. Ich habe alles andere außer, wenn es darum geht, die getTranslation-Methode dafür zu machen. Aus irgendeinem seltsamen Grund kann ich es einfach nicht herausfinden. Wenn mir jemand ein paar Ideen geben könnte, würde ich das schätzen.Englisch zu Pig Latin C# Konsolenanwendung

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace W15M5A2_CPigLatinApp 
{ 
class W15M5A2_CPigLatinAppProgram 
{ 
    static void Main(string[] args) 
    { 
     string inputPhrase = ""; 
     string[] phraseOut; 

     DisplayInfo(); 
     inputPhrase = GetPhrase(); 
     while (inputPhrase != "") 
     { 
      Console.WriteLine(" "); 
      phraseOut = GetTranslation(inputPhrase); 
      DisplayResults(inputPhrase, phraseOut); 
      inputPhrase = GetPhrase(); 
     } 
     Console.ReadKey(); 
    } 

    public static void DisplayInfo() 
    { 
       Console.WriteLine("********************************************************" + 
      "\n*** You will be prompted to enter a string of  ***" + 
      "\n*** words. The phrase will be converted into a ***" + 
      "\n*** pseudo Pig Latin with results displayed.  ***" + 
      "\n\n*** Enter as many strings as you would like.  ***" + 
      "\n********************************************************\n\n"); 
     Console.Write("\n\n\n Press any key when you are ready to begin..."); 
     Console.ReadKey(); 
     Console.Clear(); 
    } 

    public static string GetPhrase() 
    { 
     string inputPhrase; 
     Console.WriteLine("Enter a phrase or group of words " + 
      "\nTo Exit, press the Enter key\n"); 
     inputPhrase = Console.ReadLine(); 
     return inputPhrase; 
    } 
    // GetTranslation method 
    public static string[] GetTranslation(string phraseIn) 
    { 


    } 

    public static void DisplayResults(string input, string[] output) 
    { 
     Console.Clear(); 
     Console.WriteLine("Original Phrase: " + input + "\n"); 
     Console.Write("\nNew Phrase: "); 
     foreach (string i in output) 
     { 
      Console.Write(i + " "); 
     } 
     Console.WriteLine("\n"); 
    } 

} 

}

+0

Können Sie uns zeigen, was Sie versucht haben? Bitte teilen Sie uns auch Ihre Definition von Schwein Latein mit. – Rob

Antwort

0
public static string[] GetTranslation(string phraseIn) 
     { 
      string vow = "aeiouyAEIOUY"; 
      var splitted = phraseIn.Split(new[] {" "}, StringSplitOptions.None); 
      List< string> ret = new List<string>(); 
      foreach (string split in splitted) 
      { 
       string vows = string.Empty; 
       string cons = string.Empty; 
       for (var i = 0; i < split.Length; i++) 
       { 
        char ch = split[i]; 
        if (vow.Contains(ch)) 
        { 
         vows += ch; 
        } 
        else 
        { 
         cons += ch; 
        } 
       } 
       ret.Add(cons + vows + "ay"); 
      } 


      return ret.ToArray(); 
     } 
0

Dies ist eine (wenig hacky) Lösung. Ich habe es mit den Beispielen aus Wiki getestet.

public static string ToPigLatin(string word) 
{ 
    string result = string.Empty; 
    string pigSuffixVowelFirst = "yay"; 
    string pigSuffixConstanantsFirst = "ay"; 

    string vowels = "aeiouAEIOU"; 

    if(vowels.Contains(word.First())) 
    { 
     result = word + pigSuffixVowelFirst; 
    } 
    else 
    { 
     int count = 0; 
     string end = string.Empty; 
     foreach(char c in word) 
     { 
      if (!vowels.Contains(c)) 
      { 
       end += c; 
       count++; 
      } 
      else 
      { 
       break; 
      } 
     } 

     result = word.Substring(count) + end + pigSuffixConstanantsFirst; 
    } 
    return result; 
} 

Benutzung auf eigene Gefahr :)