Pig Latin Translator. C # Homework

Create an IGPAY graphical application that allows the user to enter a word. Then, when the user clicks the button, your program will generate and display the Latin equivalent of the word. (To do this, delete the first letter of the word, then add this letter to the end of the word plus the letters “ay.” For example, the fish will become ishfay and the ball will become allbay.) Make sure the GUI is attractive and all the shortcuts, text boxes, buttons and etc. clearly marked. Hint: keep the word in the string and consider using the substring method. Also remember that the Length property for the string will tell you its length. See pages 79-80 for examples.

Here is the code I came up with. I am new to this language and am a little versed in Python, but I just don’t understand why this is throwing an “Out of Range Exception” error. I am trying to make the code accept a word and display it in Latin.

private void button1_Click(object sender, EventArgs e)
{
       string word; 
       string first;
       string rest;
       string full;
       word = textBox1.Text;
       first = word.Substring(0);
       rest = word.Substring(1, word.Length);
       full =  rest + first + "ay";
       label2.Text = full;
}
+4
source share
2 answers

Not sure why you have a downward line, good work announcing that this is homework and at least providing some work that you have done. In addition, everyone here must understand that you want to learn.

, , . Phylyp, , , , .

, . , , .

  • , , , , .

  • . .

    private void Button1_Click(object sender, EventArgs e)
    {
        const string suffix = "ay";
        string enteredString = textBox1.Text;
    
        //Check the length to make sure it is at least 2
        if(enteredString.Length < 2)
        {
            MessageBox.Show("Please enter at least 2 or more characters");
            return;
        }
    
        //We get here if 2 or more characters were entered.
        //Lets go ahead an process our string
        label2.Text = string.Format("{0}{1}{2}",
            enteredString.Substring(1),
            enteredString.Substring(0,1),
            suffix);
    }
    

EDIT. , , 1, . , , , 1, . , 0, 1 . , , . , , . , , , .

, !

+3
first = word.Substring(0, 1);
rest = word.Substring(1, word.Length - 1);

Substring() , word.Length . word.Length - 1 .

, , Substring() 1.

, .

(, Rob .Substring(1))

+1

Source: https://habr.com/ru/post/1669230/


All Articles