Вывести самое длинное слово в строке - C#
Формулировка задачи:
С клавиатуры вводится строчка. Вывести самое длинное слово в строке
Решение задачи: «Вывести самое длинное слово в строке»
textual
Листинг программы
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
public class Program
{
private const string Pattern = @"\b[\w]+\b";
private static void Main()
{
var input = Console.ReadLine();
if (!string.IsNullOrEmpty(input))
{
var matches = Regex.Matches(input, Pattern, RegexOptions.Compiled);
var maxLength = -1;
var maxWord = "";
foreach (Match m in matches)
{
if (maxLength < m.Length)
{
maxLength = m.Length;
maxWord = m.Value;
}
}
Console.WriteLine("Слово максимальной длины: {0}", maxWord);
}
Console.ReadKey();
}
}
}