英语到PigLatin c#

时间:2018-02-21 01:43:21

标签: c#

刚刚将这个Pig Latin问题作为"家庭作业"。我得到的条件是:

  1. 对于以辅音发音开头的单词,初始元音之前的所有字母都放在单词序列的末尾。然后,添加ay
  2. 对于以元音声音开头的单词,将初始元音与第一个辅音或辅音簇一起移动到单词的末尾并添加ay
  3. 对于没有辅音的单词,请添加way
  4. 经过测试:

      

    编写一个将英语句子转换为Pig Latin

    的方法

    那变成了

      

    再次离开ethodmay atthay illway onvertcay anay ishenglay entencesay ointay igpay atinlay

    它做了它应该做的事情,一个例外不在规则中,但我想到了它,我不知道如何实现它。我创建的方法完全解决了问题,但如果我尝试将所有辅音词转换成piglatin,它就不起作用了。例如,grrrrr进入piglatin应为grrrrray

    public static string ToPigLatin(string sentencetext)
        {
            string vowels = "AEIOUaeiou";
            //string cons = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
            List<string> newWords = new List<string>();           
    
            foreach (string word in sentencetext.Split(' '))
            {
                if (word.Length == 1)
                {
                    newWords.Add(word + "way");
                }
    
                if (word.Length == 2 && vowels.Contains(word[0]))
                {
                    newWords.Add(word + "ay");
                }
    
                if (word.Length == 2 && vowels.Contains(word[1]) && !vowels.Contains(word[0]))
                {
                    newWords.Add(word.Substring(1) + word.Substring(0, 1) + "ay");
                }
    
                if (word.Length == 2 && !vowels.Contains(word[1]) && !vowels.Contains(word[0]))
                {
                    newWords.Add(word + "ay");
                }                
    
                for (int i = 1; i < word.Length; i++)
                {
                    if (vowels.Contains(word[i]) && (vowels.Contains(word[0])))
                    {
                        newWords.Add(word.Substring(i) + word.Substring(0, i) + "ay");
                        break;
                    }
                }
    
                for (int i = 0; i < word.Length; i++)
                {
                    if (vowels.Contains(word[i]) && !(vowels.Contains(word[0])) && word.Length > 2)
                    {
                        newWords.Add(word.Substring(i) + word.Substring(0, i) + "ay");
                        break;
                    }
    
                }
            }            
                return string.Join(" ", newWords);          
    
        }
    
        static void Main(string[] args)
        {
               //Console.WriteLine("Enter a sentence to convert to PigLatin:");
               // string sentencetext = Console.ReadLine();
                string pigLatin = ToPigLatin("Write a method that will convert an English sentence into Pig Latin");
                Console.WriteLine(pigLatin);
    
                Console.ReadKey();
    
        }
    

1 个答案:

答案 0 :(得分:2)

放手一搏:

public static string ToPigLatin(string sentencetext)
{
    string vowels = "AEIOUaeiou";
    string cons = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";

    Func<string, string> toPigLatin = word =>
    {
        word = word.ToLower();

        var result = word;

        Func<string, string, (string, string)> split = (w, l) =>
        {
            var prefix = new string(w.ToArray().TakeWhile(x => l.Contains(x)).ToArray());
            return (prefix, w.Substring(prefix.Length));
        };

        if (!word.Any(w => cons.Contains(w)))
        {
            result = word + "way";
        }
        else
        {
            var (s, e) = split(word, vowels);
            var (s2, e2) = split(e, cons);
            result = e2 + s + s2 + "ay";
        }
        return result;
    };

    return string.Join(" ", sentencetext.Split(' ').Select(x => toPigLatin(x)));
}

代码:

string pigLatin = ToPigLatin("Grrrr Write a method that will convert an English sentence into Pig Latin");
Console.WriteLine(pigLatin);

给出:

  

grrrray itewray away ethodmay atthay illway onvertcay anay ishenglay entencesay ointay igpay atinlay