From cbce1956e1db38a591ebe8aaf53d5627e96a1a13 Mon Sep 17 00:00:00 2001 From: La Programmatrice Verde Date: Thu, 27 Feb 2025 09:57:03 +0100 Subject: [PATCH] Opzione 3 --- Program.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index 7737d2b..221c167 100644 --- a/Program.cs +++ b/Program.cs @@ -41,7 +41,12 @@ class Program { break; case 3: Console.Clear(); - + if (array != null) { + Console.WriteLine($"La frase più lunga è {MaxLength(array)}"); + } + else { + Console.WriteLine("Errore: è necessario creare l'array prima di mostrare la frase più lunga."); + } Pausa(); break; case 4: @@ -94,4 +99,16 @@ class Program { } } + static string MaxLength(string[] p_array) { + int max = 0, maxIndex = 0; + for (int i = 0; i < p_array.Length; i++) { + if (p_array[i].Length > max) { + max = p_array[i].Length; + maxIndex = i; + } + } + + return p_array[maxIndex]; + + } } \ No newline at end of file