C# Program to Reverse a String

C# Program to Reverse a String – Learn how to reverse string using string handling functions and normal programs with Examples.

For example, “VISWANATH” is the input string. After reversing the input string i,e, reverse of input string is “HTANAWSIV”.

Reverse string Examples:
Input string : ABSC
Output String: CSBA

Input string : MALAYALAM
Output String: MALAYALAM

C# program to reverse a String

Example Program 1 – Reversing a string using string handling function.

class Program
    {
        static void Main(string[] args)
        {
            //Input String to Reverse
          
             string s="VISWANATH";
             char[] arr=s.ToCharArray();

            Console.WriteLine("input String Before Reverse : " +s);

            Array.Reverse(arr);

            Console.Write("Input string After Reverse : ");
            Console.Write(arr);


        }
    }

Output
input String Before Reverse : VISWANATH
Input string After Reverse : HTANAWSIV

Example Program 2 Reversing a string using for loop.

class Program
    {
        static void Main(string[] args)
        {
            //Input String to Reverse
          
             string s="VISWANATH";
             char[] arr=s.ToCharArray();
             char[] revarr = new char[arr.Length];

            Console.WriteLine("input String Before Reverse : " +s);

            int strlen = arr.Length;
            int a = 0;

            for (int i = strlen-1; i >=0; i--)
            {
                revarr[a] = arr[i];
                a++;
                
            }

            Console.Write("Input String After Reverse : ");
            Console.Write(revarr);
        }
    }

Output
input String Before Reverse : VISWANATH
Input String After Reverse : HTANAWSIV

Example Program 3 Reversing a string using for loop with half of the string length iterations.

class Program
    {
        static void Main(string[] args)
        {
            //Input String to Reverse
          
             string s="VISWANATH";
             char[] arr=s.ToCharArray();
             char[] revarr = new char[arr.Length];

            Console.WriteLine("input String Before Reverse : " +s);

            int strlen = arr.Length;
             

            for (int i = 0; i < strlen; i++)
            {
                revarr[i] = arr[--strlen];
                revarr[strlen]=arr[i];
             
                
            }

            Console.Write("Input String After Reverse : ");
            Console.Write(revarr);
        }
    }

Output
input String Before Reverse : VISWANATH
Input String After Reverse : HTANAWSIV

Example Program 4 Reversing a string using Recursion.

class Program
    {
        static void Main(string[] args)
        {
            //Input String to Reverse
          
             string s="VISWANATH";
             char[] arr=s.ToCharArray();
             char[] revarr = new char[arr.Length];

            Console.WriteLine("input String Before Reverse : " +s);

            int strlen = arr.Length;
             
            Program p=new Program();
            p.reverse_String( arr, 0, strlen - 1);
         

            Console.Write("Input String After Reverse : ");
            Console.Write(arr);
        }

        private void reverse_String( char[] Str, int i, int len)
        {
            char temp;
            temp = Str[i];
            Str[i] = Str[len - i];
            Str[len - i] = temp;

            if (i == len / 2)
            {
                return;
                
            }
            reverse_String( Str, i + 1, len);
        }
        
    }

Output

input String Before Reverse : VISWANATH
Input String After Reverse : HTANAWSIV

Related Posts