Reputation: 73
Create a GUI application called IGPAY that lets a user enter a word. Then, when the user clicks a button, your program will generate and display the Pig Latin equivalent of that word. (To do this you remove the first letter of the word, then add that letter to the end of the word plus the letters "ay." For example, fish would become ishfay and ball would become allbay.) Make sure the GUI is attractive in appearance and that all labels, textboxes, buttons, and similar are clearly labeled. Hint: store the word in a string and consider using the Substring method. Also remember that the Length property of a string will tell you its length. See pages 79-80 of the text for examples.
Here is the code I came up with. I am new to this language and have a little knowledge with Python, but I'm just not understanding why it throws me an "Out of range exception" error. I am trying to make it so the code accepts any word and displays it in pig 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;
}
Upvotes: 4
Views: 3064
Reputation: 1689
first = word.Substring(0, 1);
rest = word.Substring(1, word.Length - 1);
Since the Substring()
method is zero-based, word.Length
is outside the string. Changing the second argument to word.Length - 1
should avoid the error.
Also, since you need just the first character, the first call to Substring()
should have the second argument as 1.
Good you've called this out as homework and have given your attempted code.
(Edited to incorporate Rob's very correct comment about .Substring(1)
)
Upvotes: 1
Reputation: 1814
Not sure why you got a downvote, good job declaring this is homework and at least providing some work that you have done. Aside from that, everybody here should appreciate you are wanting to learn.
There are some things I would consider if I were you. In addition to the answer from Phylyp which should fix your exception, you should also handle if the user enters less than two characters, which can also cause exception.
Below I show an example of three things you can do. Not saying it is the best way but it is an option.
Check the entered string to make sure it is at least two characters and if not, prompt user.
Cut down on the amount of string variables and lines of declaration by using one string.Format call.
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 - I have edited the answer above to what you actually want to have which is the first substring of just 1 and that will give you everything in the string after the first letter. Basically, you are saying give me the string starting at position 1, or the second letter. Then, the second variable says give me the string starting at position 0 and only for a length of 1 character. This will essentially give you only the first letter. So, this will give you what you want. Couple that with checking to make sure at least two characters are entered, and you shouldn't have any exceptions.
Hope this helps!
Upvotes: 3