Program to find the LONGEST word in an entered String.
You may also like this post too :Find The Longest Word in String Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Program { static void Main(string[] args) { string text; string[] stringList; int ni = 0, len, max = 0; Console.Write("Enter a string :"); text = Console.ReadLine(); stringList = text.Split(' '); len = stringList.Length; //finding index of longest string for (int i = 0; i < len; i++) { if(stringList[i].Length>max) { max = stringList[i].Length; ni = i; } } //Output Console.WriteLine("Longest string:{0} \n Count of count of chrackter:{1}",stringList[ni],max); Console.ReadLine(); } } |
Output
Program to find longest word in a string in C#
C# program to find longest string
C# program to find longest word in a string