-3

I am just trying to find out the reverse and length of a string. Here is the snippet of the Code:

    Console.WriteLine("Please Enter your String: ");
        string InputString = Console.ReadLine();

        char[] String_Array = InputString.ToCharArray();
        int Length = 0;

        foreach (char c in String_Array)
        {
            Length++;
        }
        char[] String_Reverse = null;
        for (int i = Length - 1, j = 0; Length >= 0; Length--, j++)
        {
            //Here is the error I am facing, Null Reference Exception

            String_Reverse[j] = String_Array[i];
        }
        string Reverse = new string(String_Reverse);

        Console.WriteLine("Length of the String " + InputString + " is :" + Length + " and Reverse of the String is :" + Reverse);
        Console.ReadLine();
Moranis
  • 17
  • 8

1 Answers1

1

You never actually initialize String_Reverse as an array

Also, instead of using a foreach loop to get the length of an array you can use String_Array.Length this will return the length of the array without having to loop over each item in the array.

Gilgamesh
  • 680
  • 6
  • 17