简体   繁体   中英

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. 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:

      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. 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.

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. For a future FYI, you could use a Dictionary if you need to know the exact counts, also.

    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. 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.)

Perhaps you're missing an inner for loop? Currently you assign something ( enteredword[index] ) to letter , and then immediately compare them. 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);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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