How to find the largest and the smallest word in a string with C#

1 Answer

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "csharp c cpp python java";

            string[] words = s.Split(' ');

            string max = words[0], min = words[0];

            foreach (string w in words)
            {
                if (w.Length > max.Length)
                    max = w;
                if (w.Length < min.Length)
                    min = w;
            }
            Console.WriteLine("The largest word is: {0}", max);
            Console.WriteLine("The smallest word is: {0}", min);
        }
    }
}

/*
run:
   
The largest word is: csharp
The smallest word is: c
      
*/

 



answered Mar 20, 2017 by avibootz

Related questions

...