Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a program to find the longest word in a text.
- namespace _14.LongestWordInText
- {
- using System;
- public class LongestWordInText
- {
- public static void Main(string[] args)
- {
- checked
- {
- Console.WriteLine("Enter your Text on single line!");
- string inputText = Console.ReadLine();
- char[] separators = new char[] { ' ', ',', ';', '.', '!', '?', ':', '\"', '-', '/', '(', ')', '@', '#', '%', '$' };
- string[] text = inputText.Split(separators, StringSplitOptions.RemoveEmptyEntries);
- if (text.Length > 0)
- {
- int maxLenght = 0;
- string longestWord = string.Empty;
- for (int i = 0; i < text.Length; i++)
- {
- int currentLenght = text[i].Length;
- if (currentLenght > maxLenght)
- {
- maxLenght = currentLenght;
- longestWord = text[i];
- }
- }
- Console.WriteLine("The Longest Word in that Text is: {0} !", longestWord);
- }
- else
- {
- Console.WriteLine("Error! - Text not exist!!!");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement