Reverse string in c#
I have a string and want to order that string in reverse. We can write this reverse in several ways.
For example, I have a string "Hello, world" and want to see this in reverse order "dlrow ,olleH".
when a string is same as if it reverse. for example Madam is a string and when it reverse also a madam. this is called palindrome.
A program for String reverse.
class Program
{
public static void Main()
{
Console.WriteLine("Please enter a string to reverse");
string reverse = Console.ReadLine();
char[] reverseArray = reverse.ToCharArray();
for (int j = 0; j < reverseArray.Length / 2; j++)
{
char s;
s = reverseArray[j];
reverseArray[j] = reverseArray[reverseArray.Length - j - 1];
reverseArray[reverseArray.Length - j - 1] = s;
}
for (int i = 0; i < reverseArray.Length; i++)
Console.Write(reverseArray[i]);
Console.ReadLine();
}
}
Program for Palindrome
class Program
{
public static void Main()
{
Console.WriteLine("Please enter a string to reverse");
string reverse = Console.ReadLine();
char[] reverseArray = reverse.ToCharArray();
for (int j = 0; j < reverseArray.Length / 2; j++)
{
char s;
s = reverseArray[j];
reverseArray[j] = reverseArray[reverseArray.Length - j - 1];
reverseArray[reverseArray.Length - j - 1] = s;
}
string reverseString=string.Empty;
for (int i = 0; i < reverseArray.Length; i++)
{
reverseString=reverseString+reverseArray[i].ToString();
Console.Write(reverseArray[i]);
}
if (reverseString == reverse)
Console.WriteLine("Palindrome");
else
Console.WriteLine("not a Palindrome");
Console.ReadLine();
}
}