How to reverse a String using C#?

CsharpProgrammingServer Side Programming

To reverse a string, use the Array.Reverse() method.

Set the string you want to reverse −

string str = "Amit";

In the above method, we have converted the string into character array −

char[] ch = str.ToCharArray();

Then the Reverse() method is used.

Array.Reverse(ch);

Example

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {

         string str = "Amit";
         char[] ch = str.ToCharArray();

         Array.Reverse(ch);

         foreach(var items in ch) {
            Console.WriteLine(items);
         }

         Console.ReadLine();
      }
   }
}
raja
Published on 14-Aug-2018 13:02:11
Advertisements