How to Reverse String using For Loop

In this blog, I am going to explain the program for string reverse using For Loop, without the use of any predefined function.

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
  1. using System;

  2. namespace StringReverseUingForLoop {
  3. public static class StringReverse {
  4. // Reverse ExtentionMethod
  5. public static string Reverse(this string str) {
  6. // calling StringReverseUingForLoop
  7. return Program.StringReverseUingForLoop(str);
  8. }
  9. }
  10. class Program {
  11. static void Main() {
  12. Console.WriteLine("(Input is Optional and Default value will be CSharpcorner)");
  13. Console.WriteLine("Enter your data to Reverse otherwise just Hit Enter");
  14. string sampleText = Console.ReadLine();
  15. if (string.IsNullOrEmpty(sampleText))
  16. sampleText = "https://www.c-sharpcorner.com";
  17. Console.WriteLine("=======================================================");
  18. Console.WriteLine($"Original Text : {sampleText}");
  19. Console.WriteLine("=======================================================");
  20. var reverseText = StringReverseUingForLoop(sampleText);
  21. Console.WriteLine($"Reversed Text (Using Normal Method) : {reverseText}");
  22. Console.WriteLine("======================================================");
  23. reverseText = sampleText.Reverse();
  24. Console.WriteLine($"Reversed Text (Using Extension Method) : {reverseText}");
  25. }
  26. public static string StringReverseUingForLoop(string str) {
  27. var reversedStr = "";
  28. for(int i = str.Length - 1; i >= 0; i--)
  29.     reversedStr += str[i];
  30. return reversedStr;
  31. }
  32. }
  33. }
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.