Finding the Longest Word in String Array using foreach loop
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Program { static void Main(string[] args) { string[] arr = { "Chsarp", "Console","Examples","www.csharp-console-examples.com" }; string longWord = ""; int Wordcount = 0; foreach (string item in arr) { if (item.Length > Wordcount) { Wordcount = item.Length; longWord = item; } } Console.WriteLine("The longest word: {0} \nLetters count : {1}", longWord, Wordcount); Console.ReadKey(); } } |
Output:
Online Code: