Exemple #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 ShortestWord FindShortestWord(string phrase)
        {
            ShortestWord shortestWord = new ShortestWord();

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

            phrase = phrase.TrimEnd();

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

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