C#C
C#8mo ago
Pavan

C# PigLatin exercise : tests failing for Rule 3 and Rule 4

I am writing a Translate function :
Rule 3:If a word starts with zero or more consonants followed by "qu", first move those consonants (if any) and the "qu" part to the end of the word, and then add an "ay" sound to the end of the word.

For example:
"quick" -> "ickqu" -> "ickquay" (starts with "qu", no preceding consonants)
"square" -> "aresqu" -> "aresquay" (starts with one consonant followed by "qu")

Test case failed :
Expected: "eenquay"
Actual: "ueenqay"

Rule 4:If a word starts with one or more consonants followed by "y", first move the consonants preceding the "y"to the end of the word, and then add an "ay" sound to the end of the word.

Some examples:
"my" -> "ym" -> "ymay" (starts with single consonant followed by "y")
"rhythm" -> "ythmrh" -> "ythmrhay" (starts with multiple consonants followed by "y")
Test case failed :
Expected: "ymay"
Actual: "myay"

This is my code
using System;
using System.Text.RegularExpressions;

public static class PigLatin
{
    public static string Translate(string word)
    {
        word = word.ToLower();
        //Rule 1
        if(Regex.IsMatch(word, @"^(xr|yt|[aeiou])"))
        {
            return word + "ay";
        }

        //Rule 2
        var match = Regex.Match(word, @"^([^aeiou]+)(.*)");
        if(match.Success)
        {
            return match.Groups[2].Value + match.Groups[1].Value + "ay";
        }

        //Rule 3
        var match2 = Regex.Match(word, @"^([^aeiou]*qu)(.*)");
        if (match2.Success)
        {
            return match2.Groups[2].Value + match2.Groups[1].Value + "ay";
        }
        
        //Rule 4
        var match3 = Regex.Match(word, @"^([^aeiou]+)(y.*)");
        if (match3.Success)
        {
            return match3.Groups[2].Value + match3.Groups[1].Value + "ay";
        }
        return word + "ay";

    }
}

What I am doing wrong?
image.png
Was this page helpful?