Introduction
In this blog, I am going to explain the program for string reverse using only For Loop.
Software Requirements
- C# 3.0 or higher,
- Visual Studio or Notepad
Program
- using System;
-
- namespace StringReverseUingForLoop {
- public static class StringReverse {
-
- public static string Reverse(this string str) {
-
- return Program.StringReverseUingForLoop(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 = "https://www.c-sharpcorner.com";
- Console.WriteLine("=======================================================");
- Console.WriteLine($"Original Text : {sampleText}");
- Console.WriteLine("=======================================================");
- var reverseText = StringReverseUingForLoop(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 StringReverseUingForLoop(string str) {
- var reversedStr = "";
- for(int i = str.Length - 1; i >= 0; i--)
- reversedStr += str[i];
- return reversedStr;
- }
- }
- }
Now run the code.
If User does not enter anything, then the default value will be "https://www.c-sharpcorner.com." The following example is without input.
If User enters "Microsoft Technologies," it should look like the following:
Thank you so much for reading my blog.