简体   繁体   English

C#在字符串中显示重复的字母

[英]C# Display repeated letter in string

I'm trying to figure out how to display the repeated letter in a string. 我正在试图弄清楚如何在字符串中显示重复的字母。 For example if the entered string was "red house" it would show that the letter "e" was repeated, then it would be displayed in the label I created. 例如,如果输入的字符串是“red house”,它将显示重复字母“e”,然后它将显示在我创建的标签中。 This is a basic program, Here is what I've written thus far. 这是一个基本程序,这是我到目前为止所写的内容。 Any help is appreciated. 任何帮助表示赞赏。

private void button1_Click(object sender, EventArgs e)
    {
        string enteredWord = textBox1.Text;
        char letter;


        for (int index = 0; index < enteredWord.Length; index++) 
        {
            letter = enteredWord[index];

            if (letter == enteredWord[index])
            {
                label1.Text = (letter.ToString());
            }
            else
            { return; 

            }

You could also use Linq for that: 你也可以使用Linq

      var query = from l in enteredWord
                    group l by l into g
                    where g.Count() > 1
                    select new {  letter = g.Key, count = g.Count() };
        foreach (var item in query)
        {
            label1.Text += item.letter + " ";
        }

This should do what you're looking for: 这应该做你想要的:

    public static Dictionary<char, int> Count(string input)
    {
        Dictionary<char, int> d = new Dictionary<char, int>();
        foreach (char c in input)
        {
            if (d.Keys.Contains(c))
                d[c]++;
            else
                d.Add(c, 1);
        }
        return d;
    }

    static void Main(string[] args)
    {
        Dictionary<char, int> d = Count("Red House");
        foreach (char c in d.Keys)
        {
            Console.WriteLine("{0}: {1}", c, d[c]);
        }
        Console.ReadKey();
    }

Could build a HashSet and check each letter. 可以构建一个HashSet并检查每个字母。 I'm sure there's a more efficient way, but this should work: (untested) 我确信有一种更有效的方法,但这应该有效:(未经测试)

string enteredWord = textBox1.Text;
HashSet<char> letters = new HashSet<char>();
foreach(char c in enteredWord)
{
    if (letters.Contains(c))
        label1.Text += c.ToString();
    else
        letters.Add(c);
}

EDIT: I suppose this will print out duplicates of the letters if they appear 3 or more times, but you get the idea. 编辑:我想如果它们出现3次或更多次,这将打印出重复的字母,但你明白了。

I would suggest using a List and add the value if you have not encountered it, and update the textbox if you have already encountered it. 我建议使用List并在没有遇到它的情况下添加值,如果已经遇到它,请更新文本框。 For a future FYI, you could use a Dictionary if you need to know the exact counts, also. 对于未来的FYI,如果您需要知道确切的计数,也可以使用词典

    List<char> charactersThatHaveOccurred = new List<char>();
    string enteredWord = textBox1.Text;
    foreach(var character in enteredWord)
    {
        if(charactersThatHaveOccurred.Contains(character ))
            label1.Text += character ;
        else
            charactersThatHaveOccurred.Add(character );
    }

The first thing that comes to mind is: 首先想到的是:

List<char> repeats = enteredWord.Where(c1 => enteredWord.Count(c2 => c1 == c2) > 1).Distinct().ToList();

That will return a List<char> of all the repeated characters. 这将返回所有重复字符的List<char> You can grab the first one, or whatever you want. 您可以抓住第一个或任何您想要的东西。

It may not be optimal in terms of speed, but it's simple. 它在速度方面可能不是最佳的,但它很简单。

See other answers for better ways to solve this problem, but I'm trying to see what you were trying to do. 有关解决此问题的更好方法,请参阅其他答案,但我正在尝试查看您尝试执行的操作。 (Note, the code below is very inefficient, using a HashSet of already seen characters would be my solution at it.) (注意,下面的代码是非常低效的,使用已经看到的字符的HashSet将是我的解决方案。)

Perhaps you're missing an inner for loop? 也许你错过了一个内在的for循环? Currently you assign something ( enteredword[index] ) to letter , and then immediately compare them. 目前您为letter分配了一些东西( enteredword[index] ),然后立即进行比较。 So you're comparing each letter to itself. 所以你要把每个字母都比作自己。

something like 就像是

    for (int index = 0; index < enteredWord.Length; index++) 
    {
        letter = enteredWord[index];

        for (int index2 = 0; index < enteredWord.Length; index++)
        {
            if (index != index2 && letter == enteredWord[index2])
            {
                label1.Text = (letter.ToString());
                return;
            }
        }
    }
           string s = "red house";   
            foreach (char c in s)
            {
                if (s.IndexOf(c) != s.LastIndexOf(c))
                {
                    label1.Text += c.ToString();
                }
                s.Replace(c.ToString(), string.Empty);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM