Find First repeated and unrepeated character in a string

       protected void Button1_Click(object sender, EventArgs e)
        {
            string Inputstring = TextBox1.Text.ToString();
            string notRepeatedChar = string.Empty; string repeatedChar = string.Empty;
            bool statusRepeated = false, statusNotRepeated = false;

            for (int i = 0; i < Inputstring.Length - 1; i++)
            {
                if (!statusRepeated || !statusNotRepeated)
                {
                    string s = Inputstring[i].ToString();
                    for (int j = i+1; j <= Inputstring.Length - 1; j++)
                    {
                        if (s == Inputstring[j].ToString())
                        {
                            repeatedChar = s;
                            if (!statusRepeated)
                            {
                               
                                Label1.Text = " '" + s + "' " + " is the first repeated character.";
                                statusRepeated = true;
                               
                            }

                        }
                        else if (j == Inputstring.Length - 1)
                        {
                            notRepeatedChar = s;
                            if (!statusNotRepeated)
                            {
                                if (!repeatedChar.Equals(notRepeatedChar))
                                {
                                    Label2.Text = " '" + notRepeatedChar + "' " + " is the first un-repeated character.";
                                    statusNotRepeated = true;
                                }
                            }
                        }

                    }

                }
            }
        }

No comments:

Post a Comment