简体   繁体   中英

Calculating the Largest and smallest Letter through its ASCII value C#

Hey im having some trouble with a school assignment. The issue is that I have a foreach loop searching through the userinput. It should display the Largest character or smallest, but when i insert Hello! it displays the largest value being !. The example states its supposed to be the lowercase o. Here is my code. Forgive me if this looks messy, first time on this website, and this needs to be done quick.

This is all being done in a windows forum app in visual studio.

        if (UI_TB_UserInput.Text.Length > 0)
        {

            foreach (char c in UI_TB_UserInput.Text)
            {

                char min = UI_TB_UserInput.Text[0];//?
                char max = UI_TB_UserInput.Text[0];//?

                if (c < min)
                {

                    min = c;

                }
                if (c > max)
                {

                    max = c;

                }

                if (UI_RB_Min.Checked)
                {
                    UI_LB_MinMaxOutput.Text = min.ToString();
                }
                else
                {
                    UI_LB_MinMaxOutput.Text = max.ToString();
                }
            }

        }
        else
        {
            UI_LB_MinMaxOutput.Text = "";//If not > 0 then display blank
        }

Move your char min = UI_TB_UserInput.Text[0]; and char max = UI_TB_UserInput.Text[0]; to before the loop starts. The way you have it, the min and max get set to the first character every iteration of the loop. Then it compares the character it is currently looking at to UI_TB_UserInput.Text[0] rather than what you intended to compare it to. You want this:

if (UI_TB_UserInput.Text.Length > 0)
    {
    char min = UI_TB_UserInput.Text[0];
    char max = UI_TB_UserInput.Text[0];

        foreach (char c in UI_TB_UserInput.Text)
        {
            if (c < min)
            {

                min = c;

            }
            if (c > max)
            {

                max = c;

            }

            if (UI_RB_Min.Checked)
            {
                UI_LB_MinMaxOutput.Text = min.ToString();
            }
            else
            {
                UI_LB_MinMaxOutput.Text = max.ToString();
            }
        }
    }
    else
    {
        UI_LB_MinMaxOutput.Text = "";//If not > 0 then display blank
    }

Why loop thru the string... Use the available features of arrays...

with a button_click on the form....

private void button1_Click(object sender, EventArgs e)
{
  if (UI_TB_UserInput.Text.Length > 0)
  {
    string inputString = UI_TB_UserInput.Text;
    char[] charArray = inputString.ToCharArray();
    Array.Sort(charArray); // array sorted from low to high
    Array.Reverse(charArray); // reverse order to get high to low
    UI_LB_MinMaxOutput.Text = charArray[0].ToString();
  }
}

I think you missed out the important part of the question ASCII value . You would have to get the ASCII values of the string first and then check its value. Here is the code working for me.

if (UI_TB_UserInput.Text.Length > 0)
{
    byte[] asciiBytes = Encoding.ASCII.GetBytes(UI_TB_UserInput.Text);
    byte minByte = asciiBytes[0];
    byte maxByte = asciiBytes[0];
    foreach (byte i in asciiBytes)
    {
        if (i < minByte)
        {
            minByte = i;
        }
        if (i > maxByte)
        {
            maxByte = i;
        }

    }
    if (UI_RB_Min.Checked)
    {
        UI_LB_MinMaxOutput.Text = ((char)minByte).ToString();
    }
    else
    {
        UI_LB_MinMaxOutput.Text = ((char)maxByte).ToString();
    }
}
else
{
    UI_LB_MinMaxOutput.Text = "";//If not > 0 then display blank
}

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