I Albu
I Albu

Reputation: 31

English to PigLatin c#

Just had this Pig Latin problem as "homework". The conditions I have been given are:

  1. For words that begin with consonant sounds, all letters before the initial vowel are placed at the end of the word sequence. Then, ay is added.
  2. For words that begin with vowel sounds move the initial vowel(s) along with the first consonant or consonant cluster to the end of the word and add ay.
  3. For words that have no consonant add way.

Tested with:

Write a method that will convert an English sentence into Pig Latin

That turned into

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

It does what it should with one exception which is not in the rules but I thought about it and I have no idea how I can implement it. The method I created does exactly what the problem is asking but if I try to convert an all consonants word into piglatin it does not work. For example grrrrr into piglatin should be 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();

    }

Upvotes: 3

Views: 1805

Answers (1)

Enigmativity
Enigmativity

Reputation: 117064

Give this a go:

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)));
}

The code:

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

gives:

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

Upvotes: 2

Related Questions