using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class ReverseString
{
// Reverse the String using While loop
public void Using_Whileloop()
{
string Get_string, Rev_string = "";
int x;
Get_string = Console.ReadLine();
x = Get_string.Length - 1;
while (x >= 0)
{
Rev_string = Rev_string + Get_string[x];
x--;
}
Console.WriteLine("The Given String is :::> " + Get_string);
Console.WriteLine("The Reverse String is :::> " + Rev_string);
}
// Reverse the String using do While loop
public void Using_do_whileloop()
{
string Get_string, Rev_string = "";
int x;
Get_string = Console.ReadLine();
x = Get_string.Length - 1;
do
{
Rev_string = Rev_string + Get_string[x];
x--;
} while (x >= 0);
Console.WriteLine("The Given String is :::> " + Get_string);
Console.WriteLine("The Reverse String is :::> " + Rev_string);
}
// Reverse the String using For loop
public void Using_Forloop()
{
string Get_string, Rev_string = "";
int x;
Get_string = Console.ReadLine();
for (x = Get_string.Length - 1; x >= 0; x--)
{
Rev_string = Rev_string + Get_string[x];
}
Console.WriteLine("The Given String is :::> " + Get_string);
Console.WriteLine("The Reverse String is :::> " + Rev_string);
}
//Main Function
static void Main()
{
ReverseString rs = new ReverseString();
rs.Using_do_whileloop();
rs.Using_Forloop();
rs.Using_Whileloop();
Console.ReadKey();
}
}
}I will explain how to reverse the string using various loops such as while , do while, and for. Hope this code will be useful for your project.
