1

I have this method that gives me IndexOutOfRangeException, can you guys help me to understand why?

public string FlipString(string inTxt)
    {
        StringBuilder outTxt = new StringBuilder();
        for (int i = inTxt.Length; i > 0; i--)
        {
            char ch = inTxt[i];
            outTxt.Append(ch);
        }
        Console.WriteLine(outTxt.ToString());
        return outTxt.ToString();
    }

The method has to be written like this (without the exception)

6

7 Answers 7

4

Arrays in C# are 0-based, not 1-based. You are iterating from n to 1, but you need to iterate from n-1 to 0:

for (int i = inTxt.Length - 1; i >= 0; i--)

On the other hand, if you just want to reverse the string, there's a simpler solution using LINQ:

var reversed = new String(inTxt.Reverse().ToArray())
2
  • I now that there is a lot better/easier ways to do this but it have to be written like this
    – MrProgram
    Nov 6, 2012 at 10:23
  • @user1789325: I thought as much, which is why I also answered your original question instead of just providing an alternative. ;-)
    – Heinzi
    Nov 6, 2012 at 10:36
1

to reverse jus try this

string reversed = new string(stringinput.Reverse().ToArray());

or

string input = "hello world";
char[] inputarray = input.ToCharArray();
Array.Reverse(inputarray);
string output = new string(inputarray);
0

Try this

public string FlipString(string inTxt)
{
    StringBuilder outTxt = new StringBuilder();
    for (int i = inTxt.Length-1; i >= 0; i--)
    {
        char ch = inTxt[i];
        outTxt.Append(ch);
    }
    Console.WriteLine(outTxt.ToString());
    return outTxt.ToString();
}

You need to make your first entry into the loop be for the length of your string - 1 as the string array is zero based.

0
public string FlipString(string inTxt)
{
    StringBuilder outTxt = new StringBuilder();
    for (int i = inTxt.Length - 1; i >= 0; i--)
    {
        char ch = inTxt[i];
        outTxt.Append(ch);
    }
    Console.WriteLine(outTxt.ToString());
    return outTxt.ToString();
}
0

Indexes in strings begin in 0 and get to Length - 1.

First operation is

char ch = inTxt[inTxt.Length]

So your code should be

public string FlipString(string inTxt)
{
    StringBuilder outTxt = new StringBuilder();
    for (int i = inTxt.Length - 1; i > 0; i--)
    {
         char ch = inTxt[i];
         outTxt.Append(ch);
    }
    Console.WriteLine(outTxt.ToString());
    return outTxt.ToString();
}
0

Extension method:

public static string Reverse(this string source)
{
    char[] arr = source.ToCharArray();
    Array.Reverse(arr);
    return new string(arr);
}

and using:

var reverted = source.Reverse();

p.s. keep it simply as much as you can.

0

You need to start from the last character, that is the length of the string minus 1 because the string is Zero based.

public string FlipString(string inTxt)
    {
        StringBuilder outTxt = new StringBuilder();
        for (int i = inTxt.Length-1; i >= 0; i--)
        {
            char ch = inTxt[i];
            outTxt.Append(ch);
        }
        Console.WriteLine(outTxt.ToString());
        return outTxt.ToString();
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.