Answer to Question #272758 in C# for Holik

Question #272758

2. Display the longest word in the given sequence of words.

1
Expert's answer
2021-11-28T11:44:49-0500
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);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS