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)

MrProgram
  • 5,044
  • 13
  • 58
  • 98
  • 3
    String indexes are *zero-based*. – Jon Nov 06 '12 at 10:09
  • 1
    There is a linq method to do reverse: http://tipsandtricks.runicsoft.com/CSharp/StringReverse.html – Mr. Mr. Nov 06 '12 at 10:10
  • 1
    Here's a way of doing it using `Array.Reverse`: http://stackoverflow.com/questions/228038/best-way-to-reverse-a-string – Håvard S Nov 06 '12 at 10:10
  • Why does it have to be written like this? – default Nov 06 '12 at 10:13
  • Your code is correct but as indicated string indexes are zero based. This means that the last index in the string array is `string.Length - 1` Thus, your for loop should initalise as `for (int i = inTxt.Length -1; i >= 0; i--)`. Not the `-1` and `>=`. – Kami Nov 06 '12 at 10:14
  • Keep in mind that a .NET Char is not an "actual character" (grapheme), but a Unicode UTF-16 code unit. If you reverse a string simply by reversing the order of the Chars, surrogate pairs and combining characters will not be processed correctly. – Sebastian Negraszus Nov 06 '12 at 12:40

7 Answers7

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())
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • I now that there is a lot better/easier ways to do this but it have to be written like this – MrProgram Nov 06 '12 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 06 '12 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);
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
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.

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
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.

Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
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();
}
Danilo Vulović
  • 2,983
  • 20
  • 31
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();
    }
Ahmad
  • 12,336
  • 6
  • 48
  • 88
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();
}
MaLKaV_eS
  • 1,325
  • 3
  • 23
  • 39