Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

English to PigLatin c#

Tags:

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

    }
like image 314
I Albu Avatar asked Nov 18 '25 09:11

I Albu


1 Answers

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

like image 157
Enigmativity Avatar answered Nov 20 '25 22:11

Enigmativity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!