So, I've just started learning C#, and to be honest — I have no idea what I'm doing. I'm trying to make a string reverse app, and iv'e come up with this code:
string input = Console.ReadLine();
char[] wordArray = input.ToCharArray();
for(int i = input.Length; i >= 0; --i)
{
Console.Write(wordArray[i]);
}
I've checked a bunch of different tutorials and documentations, and as far as I can tell from most of them, this should work, but it doesn't.
Whenever I run the app, I type in the word I want to reverse, and the app crashes. It shows an error that say "Index was outside the bounds of the array". I found better ways to reverse strings online using Array.Reverse(), but i would still like to understand why this error occurred. Like I said before, I'm not quite sure what I'm doing, and I'll be happy if anyone can explain this in layman's terms.
input.Length-1for(int i = input.Length-1; i >= 0; --i)arrays indexes starts in 0 and goes to lenght -1