C# finding the shortest and longest word in a array

后端 未结 8 1324
小蘑菇
小蘑菇 2021-01-15 04:32

I am trying to find the shortest and longest string value based on length and im getting stuck. As of now the script exits after the writeline. I think the code needs some h

8条回答
  •  自闭症患者
    2021-01-15 05:06

    try this

        static void Main(string[] args)
        {
            string[] array1 = { "Cats and ratsasdfasdf", "just rats and the just catest", "rats" };
            var shortString = array1[0];
            var longString = array1[0];
    
            /*Code for Find Shortest and longest string in Array*/
            foreach (var t in array1)
            {
                if (shortString.Length > t.Length)
                    shortString = t;
                if (longString.Length < t.Length)
                    longString = t;
            }
            Console.WriteLine("shortest string is:" + shortString);
            Console.WriteLine("Longest string is:" + longString);
            Console.Read();
        }
    

提交回复
热议问题