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
- using System;
-
- namespace StringReverseUsingArrayFunction {
- public static class StringReverse {
-
- public static string Reverse(this string str) {
-
- return Program.StringReverseUsingArrayFunction(str);
- }
- }
- class Program {
- static void Main() {
- Console.WriteLine("(Input is Optional and Default value will be CSharpcorner)");
- Console.WriteLine("Enter your data to Reverse otherwise just Hit Enter");
- string sampleText = Console.ReadLine();
- if (string.IsNullOrEmpty(sampleText))
- sampleText = "CSharpcorner";
- Console.WriteLine("===============================");
- Console.WriteLine($ "Original Text : {sampleText}");
- Console.WriteLine("===============================");
- var reverseText = StringReverseUsingArrayFunction(sampleText);
- Console.WriteLine($ "Reversed Text (Using Normal Method) : {reverseText}");
- Console.WriteLine("===============================");
- reverseText = sampleText.Reverse();
- Console.WriteLine($ "Reversed Text (Using Extension Method) : {reverseText}");
- }
- public static string StringReverseUsingArrayFunction(string str) {
- char[] array = s.ToCharArray();
- Array.Reverse(array);
- return new string(array);
- }
- }
- }
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.