using System.Text.RegularExpressions;
public static void Main()
Test("latin", "atinlay");
Test("banana", "ananabay");
Test("smile", "ilesmay");
Test("string", "ingstray");
Test("angel", "angelway");
Test("astronomer", "astronomerway");
Test("SHOUT", "OUTSHAY");
Test("flying pigs", "ingflyay igspay");
Test("Hello World", "Ellohay Orldway");
Test("This is a test.", "Isthay isway away esttay.");
Test(string.Empty, string.Empty);
static string EnglishToPigLatin(string english)
var wordPattern = new Regex(@"\w+");
return wordPattern.Replace(english, new MatchEvaluator(match =>
return CopyCapitalization(word, ConvertEnglishToPigLatin(word));
static string ConvertEnglishToPigLatin(string english)
if (english.Length == 0) {
if (IsVowel(english[0])) {
var firstConsCount = english.TakeWhile(IsConsonant).Count();
if (firstConsCount > 1) {
return english.Substring(firstConsCount) + english.Substring(0, firstConsCount) + "ay";
return english.Substring(1) + english[0] + "ay";
static string CopyCapitalization(string source, string text)
if (string.IsNullOrEmpty(text)) {
if (source.All(Char.IsUpper)) {
if (Char.IsUpper(source[0])) {
return text.Substring(0, 1).ToUpper() + text.Substring(1).ToLower();
static bool IsVowel(char c) => "aeiou".Contains(c, StringComparison.InvariantCultureIgnoreCase);
static bool IsConsonant(char c) => !IsVowel(c);
static void Test(string input, string expected)
Console.WriteLine("Test: " + input);
actual = EnglishToPigLatin(input);
Console.WriteLine("PASS.\n");
actual = "Exception: " + e.Message;
Console.WriteLine("FAIL:");
Console.WriteLine(" Expected: " + expected);
Console.WriteLine(" Actual : " + actual);
Console.WriteLine($"{pass} / {pass + fail} tests passed.\n");
Console.WriteLine(fail == 0 ? "SUCCESS!" : "FAILURE!");