Reverse a string

// Online C# Editor for free // Write, Edit and Run your C# code using C# Online Compiler
   
  

using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
       var x= "hi sahir";
   
       string reverse ="";
      for (int i= x.Length-1;i>=0;i--)
      {
          reverse = reverse + x[i];
      }
       
       
       Console.WriteLine(reverse);
       Console.ReadLine();
    }
}


   public static string Reverse1(string Input)
   {
       // Converting string to character array 
       char[] charArray = Input.ToCharArray();

       // Declaring an empty string
       string reversedString = "";

       // Iterating the each character from right to left 
       for (int i = charArray.Length - 1; i >=0; i--)
       {

           // Append each character to the reversedstring.
           reversedString = reversedString+  charArray[i];
       }

       // Return the reversed string.
       return reversedString;
   }


 public static string ReverseStringWhile(string Input)
 {
     // Converting string to character array 
     char[] charArray = Input.ToCharArray();

     // Declaring an empty string
     string reversedString = "";

     int length, index;
     length = charArray.Length - 1;
     index = length;

     // Iterating the each character from right to left  
     while (index > -1)
     {

         // Appending character to the reversedstring.
         reversedString = reversedString + charArray[index];
         index--;
     }

     // Return the reversed string.
     return reversedString;
 }

   public static string ReverseStringRecursion(string input,string reversestring,int lengthofstring)
   {
       char[] charArray = input.ToCharArray();
       if (lengthofstring < 0)
       {
           return reversestring;

       }
      return ReverseStringRecursion(input, reversestring + charArray[lengthofstring], lengthofstring - 1);

   }


   public static void ReverseStringRunRecursion()
   {
       string input = Console.ReadLine();
       Console.WriteLine(  ReverseStringRecursion(input, "", input.Length - 1));
       Console.ReadLine();
   }
   
       

Comments