January 28, 2016
How to Reverse a String in C#/CSharp
To reverse a string in C#/CSharp, we first convert string to an array of characters. Then, use static method Reverse which resides in Array class to reverse the string. Example is given below.
using System;
using System.IO;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
string hello = "Hello World";
char[] array = hello.ToCharArray();
Array.Reverse(array);
Console.WriteLine(array);
}
}
}
using System;
using System.IO;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
string hello = "Hello World";
char[] array = hello.ToCharArray();
Array.Reverse(array);
Console.WriteLine(array);
}
}
}
using System; using System.IO; namespace Hello_World { class Program { static void Main(string[] args) { string hello = "Hello World"; char[] array = hello.ToCharArray(); Array.Reverse(array); Console.WriteLine(array); } } }