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 {
- // Reverse ExtentionMethod
- public static string Reverse(this string str) {
- // calling StringReverseUingForLoop
- 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;
- }
- }
- }
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.

Sourav Kumar DasPosted Nov 6, 2019, 10:32 PM
Nice useful article.
Jefferson S. MottaPosted Nov 6, 2019, 8:29 PM
Can be more simple: var word = "ecnetnes desrever a si siht"; var str = ""; foreach (var c in word) str = c + str;