Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hi! Please write a sentence and I will do the calculations: ");
            // instantiating new instance of class Sentence
            Sentence sentence = new Sentence();

            // passing the variable - to be tested
            string input = Console.ReadLine();

            LongestWord  longestWord  = sentence.FindLongestWord(input);
            ShortestWord shortestWord = sentence.FindShortestWord(input);

            Console.WriteLine("The longest word has " + longestWord.Length + " characters");
            Console.WriteLine("The longest word is: " + longestWord.Word);
            Console.WriteLine("The shortest word has " + shortestWord.Length + " characters");
            Console.WriteLine("The shortest word is: " + shortestWord.Word);
            Console.ReadKey();
        }
        public LongestWord FindLongestWord(string phrase)
        {
            LongestWord longestWord = new LongestWord();

            if (phrase is null)
            {
                throw new ArgumentNullException(nameof(phrase));
            }

            string[] words = phrase.Split(new char[] { ' ', ',', '.', '-' });

            foreach (var word in words)
            {
                if (word.Length > longestWord.Length)
                {
                    longestWord.Length = word.Length;
                    longestWord.Word   = word;
                }
            }
            return(longestWord);
        }