How To Reverse String Using Array

In this blog, I am going to explain the program for string reverse using the Array function.

Introduction

 
In this blog, I am going to explain the program for string reverse using the Array function.
 
Software Requirements
  • C# 3.0 or higher,
  • Visual Studio or Notepad
Program
  1. using System;  
  2.   
  3. namespace StringReverseUsingArrayFunction {  
  4.  public static class StringReverse {  
  5.   // Reverse ExtentionMethod  
  6.   public static string Reverse(this string str) {  
  7.    // calling StringReverseUsingArrayFunction  
  8.    return Program.StringReverseUsingArrayFunction(str);  
  9.   }  
  10.  }  
  11.  class Program {  
  12.   static void Main() {  
  13.    Console.WriteLine("(Input is Optional and Default value will be CSharpcorner)");  
  14.    Console.WriteLine("Enter your data to Reverse otherwise just Hit Enter");  
  15.    string sampleText = Console.ReadLine();  
  16.    if (string.IsNullOrEmpty(sampleText))  
  17.     sampleText = "CSharpcorner";  
  18.    Console.WriteLine("===============================");  
  19.    Console.WriteLine($ "Original Text : {sampleText}");  
  20.    Console.WriteLine("===============================");  
  21.    var reverseText = StringReverseUsingArrayFunction(sampleText);  
  22.    Console.WriteLine($ "Reversed Text (Using Normal Method) : {reverseText}");  
  23.    Console.WriteLine("===============================");  
  24.    reverseText = sampleText.Reverse();  
  25.    Console.WriteLine($ "Reversed Text (Using Extension Method) : {reverseText}");  
  26.   }  
  27.   public static string StringReverseUsingArrayFunction(string str) {  
  28.    char[] array = s.ToCharArray();  
  29.    Array.Reverse(array);  
  30.    return new string(array);  
  31.   }  
  32.  }  
  33. }  
Now run the code.
 
If user does not enter anything, then the default value will be CShaprcorner, i.e. Without Input
 
 
 
If user enters "CSharp", i.e. With Input 
 
 
 
In my next blog, we will discuss String reverse using Stack and thank you so much for reading my blog.