-4
  string myName = "Angel Hadzhiev";
        char[] CharName = myName.ToCharArray();
        Array.Reverse(CharName);
        foreach (char name in myName)
        {
            Console.WriteLine(name);
        }

        Console.ReadLine();

The problem is that the given name(Angel Hadzhiev) is not reversed when I run the Console Application. The output is this - http://prntscr.com/fvbis1

  • You can join an array of characters back into a string with the String constructor: `string newString = new String(CharName);` – spender Jul 13 '17 at 18:17

2 Answers2

6

You reverse the characters in CharName, but you write to the Console the characters in myName (which isn't reversed, and contains the original order). Your loop should have looked like this:

    foreach (char name in CharName)
    {
        Console.WriteLine(name);
    }
0

You made a simple mistake that in foeach loop instead of reversed array you are taking old array.

Please check this link for example

public static void Main(string[] args)
    {
        string myName = "Angel Hadzhiev";
        char[] CharName = myName.ToCharArray();
        Array.Reverse(CharName);
        foreach (char name in CharName)
        {
            Console.WriteLine(name);
        }
    }
Jon B
  • 51,025
  • 31
  • 133
  • 161