Reverse the string
Reverse the string by for Loop
using System;
namespace ConsoleApp
{
internal class Program
{
private static void Main(string[] args)
{
string inputString = Console.ReadLine();
string reverse = ReverseString(inputString);
Console.WriteLine(string.Format("Reverse string of {0} is {1}", inputString, reverse));
Console.ReadLine();
}
public static string ReverseString(string inputString)
{
string reveseString = "";
char[] inputStringArr = inputString.ToCharArray();
for (int i = inputStringArr.Length - 1; i <= inputStringArr.Length && i >= 0; i--)
{
reveseString += inputStringArr[i];
}
return reveseString;
}
}
}
Reverse the string by While Loop
using System;
namespace ConsoleApp
{
internal class Program
{
private static void Main(string[] args)
{
string inputString = Console.ReadLine();
string reverse = ReverseString(inputString);
Console.WriteLine(string.Format("Reverse string of {0} is {1}", inputString, reverse));
Console.ReadLine();
}
public static string ReverseString(string inputString)
{
string reveseString = "";
char[] inputStringArr = inputString.ToCharArray();
int j = inputStringArr.Length - 1;
while (j >= 0)
{
reveseString += inputStringArr[j];
j--;
}
return reveseString;
}
}
}
No comments:
Post a Comment