Opzione 3

This commit is contained in:
La Programmatrice Verde 2025-02-27 09:57:03 +01:00
parent 3338996751
commit cbce1956e1

View File

@ -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];
}
}