Advertisement
dimipan80

Advanced Topics 14. Longest Word in Text

Jul 4th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. // Write a program to find the longest word in a text.
  2.  
  3. namespace _14.LongestWordInText
  4. {
  5.     using System;
  6.  
  7.     public class LongestWordInText
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             checked
  12.             {
  13.                 Console.WriteLine("Enter your Text on single line!");
  14.                 string inputText = Console.ReadLine();
  15.  
  16.                 char[] separators = new char[] { ' ', ',', ';', '.', '!', '?', ':', '\"', '-', '/', '(', ')', '@', '#', '%', '$' };
  17.                 string[] text = inputText.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  18.                 if (text.Length > 0)
  19.                 {
  20.                     int maxLenght = 0;
  21.                     string longestWord = string.Empty;
  22.                     for (int i = 0; i < text.Length; i++)
  23.                     {
  24.                         int currentLenght = text[i].Length;
  25.                         if (currentLenght > maxLenght)
  26.                         {
  27.                             maxLenght = currentLenght;
  28.                             longestWord = text[i];
  29.                         }
  30.                     }
  31.  
  32.                     Console.WriteLine("The Longest Word in that Text is: {0} !", longestWord);
  33.                 }
  34.                 else
  35.                 {
  36.                     Console.WriteLine("Error! - Text not exist!!!");
  37.                 }
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement