2. Display the longest word in the given sequence of words.
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter sequence of words:");
string str = Console.ReadLine();
string[] split = str.Split(new char[] { ' ' });
int maxLength = 0;
string maxStr = "";
for (int i = 0; i < split.Length; i++)
{
if (split[i].Length > maxLength)
{
maxLength = split[i].Length;
maxStr = split[i];
}
}
Console.WriteLine("Longest word is:{0}",maxStr);
}
}
}
Comments
Leave a comment