using System.Text.RegularExpressions;
public static void Main()
var program = new Program();
Console.WriteLine("Exécution des tests unitaires:");
Console.WriteLine("------------------------------");
program.Test("Hello World", 5);
program.Test(" fly me to the moon ", 4);
program.Test("luffy is still joyboy", 6);
program.Test("abc de", 2);
private void Test(string s, int expected)
var result = LengthOfLastWord(s);
Console.WriteLine($"'{s}' | attendu: {expected} | reçu: {result} | {(result == expected ? "✓ RÉUSSI" : "✗ ÉCHEC")}");
private int LengthOfLastWord(string s)
(var length, var index) = (0, 0);
s = UglifyThat(s).Trim();
for (int i = 0; i < s.Length; IncrementThis(ref i))
for (int j = 0; j <= s.Length - i; IncrementThis(ref j))
var result = TestWord(s.Substring(i, j));
if (result.IsGood == "faux" && s.LastIndexOf(" ") < i)
if (result.length > length)
public (int length, string IsGood) TestWord(string s)
for (int i = 0; i < s.Length; IncrementThis(ref i))
for (int j = 1; j <= s.Length - i; IncrementThis(ref j))
var text = s.Substring(i, j);
length = text.Length > length ? text.Length : length;
if (text.Contains(' ') || text == string.Empty)
isGood = isGood.Replace("faux", "vrai");
public void IncrementThis(ref int i)
public string UglifyThat(string u)
return Regex.Replace(u, @"[^ ]", "*").Replace("*", "**");