C#找到数组中最短和最长的单词

时间:2013-08-23 03:58:48

标签: c# arrays shortest

我正在尝试根据长度找到最短和最长的字符串值,然后卡住了。截至目前,脚本在写字线后退出。我认为代码需要一些帮助,我不认为for循环可以自己工作。 任何援助将不胜感激。

        for (int i = 5; i <0; i++)
        {
            string[] word = new string[5];
           Console.WriteLine("Type in a word");
            word[i] = Console.ReadLine();

             int length = word[i].Length;
             int min = word[0].Length;
             int max = word[0].Length;
             string maxx;
             string minn;


              if (length > max)
                 {
                   maxx = word[i];
                   Console.Write("Shortest");
                  }
             if (length < min) 
              {
                 minn = word[i];
                Console.Write("Longest");
              }



         }
        Console.ReadKey(true);
    }

8 个答案:

答案 0 :(得分:16)

Linq是让你的生活更轻松的方式......

var sorted=word.OrderBy(n => n.Length);
var shortest = sorted.FirstOrDefault();
var longest = sorted.LastOrDefault();

答案 1 :(得分:5)

这是您可以使用的通用扩展方法(效率O(n)):

public static class Extensions{
    // assumes that supply a Func<T, int> that will return an int to compare by
    public static Tuple<T, T> MaxMin<T>(this IEnumerable<T> sequence, Func<T, int> propertyAccessor){
        int min = int.MaxValue;
        int max = int.MinValue;

        T maxItem = default(T);
        T minItem = default(T);

        foreach (var i in sequence)
        {
            var propertyValue = propertyAccessor(i);
            if (propertyValue > max){
                max = propertyValue;
                maxItem = i;
            }

            if (propertyValue < min){
                min = propertyValue;
                minItem = i;
            }                       
        }

        return Tuple.Create(maxItem, minItem);
}

// max will be stored in first, min in second
var maxMin = word.MaxMin(s => s.Length);

答案 2 :(得分:4)

试试这个

    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();
    }

答案 3 :(得分:1)

您的for循环条件始终为false。我从5开始,你正在检查它是否小于0 ......这总是假的,所以循环永远不会开始。

如果这只是一个错字,你也把输入放在名字[i]而不是单词[i],而名字[i]再也不用了......

答案 4 :(得分:1)

string[] word = new string[5];
for (int i = 0;i<= word.length ; i++)
            {

               Console.WriteLine("Type in a word");
                word[i] = Console.ReadLine();
            }
            int min = word[0].Length;
            int max = word[0].Length;
            string maxx="";
            string minn="";
for (int i = 0; i<=word.length ; i++)
            {
              int length = word[i].Length;   
              if (length > max)
                 {
                   maxx = word[i];

                  }
             if (length < min) 
              {
                 minn = word[i];
                Console.Write("Longest");
              }



         }
  Console.Write("Shortest:"+maxx);
  Console.Write("Longest"+minn);
  Console.ReadKey(true);
    }

根据你的代码编写这段代码。我希望这是一个正确的答案~~谢谢!!!

答案 5 :(得分:1)

如果使用LINQ,使用Max / Min方法比排序更好。

var longest = word.Max(s=>s.Length);
var shortest = word.Min(s=>s.Length);

答案 6 :(得分:0)

string[] words = new string[5];
    foreach (string word in words) {

        Console.WriteLine("Type in a word");

        word = Console.ReadLine();

        var ordered = words.Where(w=>!String.IsNullOrEmpty(w)).OrderBy(w=>w.length)

        if (ordered.First() == word) 
            Console.Write("Shortest");
        if (ordered.Last() == word)
            Console.Write("Longest");

        // First time will display both "Shortest" and "Longest" since it's the only word.
        // You may adjust code depending on what do you want to do in case of words of same length.
        Console.ReadKey(true);
    }
}

答案 7 :(得分:0)

正如@DStanley和@RenniePet指出的那样,您的原始代码并不完全符合您的目标。我相信你想循环遍历字符串数组的成员并用控制台输入填充它们。工作代码如下所示:

    string[] word = new string[5];

        for (int i = 0; i < word.Length; i++)
        {
            Console.Write("Input text: ");
            word[i] = Console.ReadLine();
        }

        List<string> _sortedWords = word.ToList<string>();
        _sortedWords.Sort((x, y) => x.Length.CompareTo(y.Length));

        Console.WriteLine("Smaller: " + _sortedWords[0]);
        Console.WriteLine("Larget: " + _sortedWords[_sortedWords.Count-1]);

        Console.ReadLine();