c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

How to reverse a string in c# ?

| | CSharp

In this article we try to implement reverse string. String is converted to char array using ToCharArray method afterthat reverse operation will happen. It is one of the interview questions. We can reverse the characters of a string likebelow. 

using System.Text; 

static void Main(string[] args)

    {

            try

            {

              Console.WriteLine("Enter the string to reverse:");

                string input = Console.ReadLine();

                char[] chars = input.ToCharArray();

                Array.Reverse(chars);

                string nameReverse = new string(chars);

                 Console.WriteLine(nameReverse);

             }

            catch(Exception ex) {

                Console.WriteLine(ex.ToString());

            }

             Console.ReadLine();

   }

 Output: 

Enter the string to reverse: 
hello world
dlrow olleh