Advertisement
Fleshian

Longest Word in Text

Apr 13th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. /*
  6.  *  Problem 14. Longest Word in a Text
  7.     Write a program to find the longest word in a text.
  8.  *  Examples:
  9.     Input                                       Output
  10.     Welcome to the Software University.         University
  11.     ------
  12.     The C# Basics course is awesome start       programming
  13.     in programming with C# and Visual Studio.  
  14.  */
  15. class LongestWord
  16. {
  17.     static void Main()
  18.     {
  19.         while (true)
  20.         {
  21.             string text = Console.ReadLine();
  22.             char[] seperators = { ' ', '.', ',' };
  23.             List<string> wordsList = text.Split(seperators, StringSplitOptions.RemoveEmptyEntries).ToList();
  24.             string longestWord = string.Empty;
  25.  
  26.             for (int i = 0; i < wordsList.Count; i++)
  27.             {
  28.                 if (wordsList[i].Length >= longestWord.Length)
  29.                 {
  30.                     longestWord = wordsList[i];
  31.                 }
  32.             }
  33.             Console.WriteLine(longestWord);
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement