Reputation: 187
I´m getting an error that is driving me crazy... i can´t find what is wrong with this code, any one will be so kind to give me some assistance?
using System;
class Program
{
static void Main(string[] args)
{
for(int i = args[0].Length; i >= 1; i--){
Console.WriteLine(args[0].Substring(i, 1));
}
}
}
A run example should be like: program.exe 6735
And the output will looks like:
5
3
7
6
So far the only thing i am getting is:
Unhandled Exception: System.ArgumentOutOfRangeException: startIndex + length > this.length Parameter name: length at System.String.Substring (Int32 startIndex, Int32 length) [0x00000] in :0 at Program.Main (System.String[] args) [0x00000] in :0
Thank you in advance for the help!
Upvotes: 0
Views: 60
Reputation: 141917
args[0].Length
is the length of your string. Since strings are 0
indexed, the length is one index past the last character in the string.
If you want to loop in reverse through a string, you should start your iterator at Length - 1
.
Upvotes: 1
Reputation: 4218
for(int i = args[0].Length; i >= 1; i--)
should be
for(int i = args[0].Length - 1; i >= 0; i--)
Upvotes: 3