-1

I'm sorry if this question is already asked on other threads but I just can't find my answer. I'm still a newbie at programming so please consider me :)

I need to create an application that lets the user enter a string and display the character that appears most frequently in the string.
1 Use a loop to select each character of the string
2 Inside the above loop, create a loop to compare the current character with each character in the a-z array
3 If a character matches, use the index to increment the current position in the integer array.
4 When the above loops have completed, loop through the integer array to find the highest count for a character

string = HELLLLOO display = L

Any help would be much appreciated :)

4

1 回答 1

2

当然,可以有更有效或更简单的方法。我只是通过 C# 编写了一个简单的解决方案,无需测试。

    string word = "HELLLOO";

    Dictionary<char, int> words = new Dictionary<char, int>();

    for(int i=0;i<word.length;i++)
    {
       if(words.ContainsKey(word[i]))
       {
          words[word[i]] = words[word[i]] + 1;
       }
       else
          words.Add(word[i],1);
    }
    char maxWord;
    int maxVal = 0;
    foreach (var item in words)
    {
      if (item.Value > maxVal)
      {
        maxVal = item.Value;
        maxWord = item.Key;
      }
    }

您可以将maxWord显示为单词中最常用的字符。

于 2013-06-10T19:23:45.073 回答