My objective is to insert text in a box, choose a value to be the shifting value on the alphabet and to get the encrypted output.
I'm just starting with C# and i found this exercise. This is what i came up with, which is certanly wrong because when I run it I get this error message "An unhandled exception of type 'System.IndexOutOfRangeException' occurred". I know where the error is but I don't know how to fix it. I'd like to know what I' doing wrong or even if the way I'm doing this is right.
Thank you in advance!
private void cifrar_Click(object sender, EventArgs e)
{
string input = message.Text;
int shift = int.Parse(shifting.Text);
char[] alphabet = new char[26] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
string output_text = "";
char[] input_array = input.ToCharArray();
for (int i = 0; i < alphabet.Length; i++)
{
for (int j = 0; j < input_array.Length; j++)
{
if (input_array[j] == alphabet[i])
input_array[j] = alphabet[i + shift];
}
}
output_text = new string(input_array);
output.Text = output_text;
}
}
}