C# – Test String for Palindrome

email me

using System;

class Program
{
public static void Main()
{
string userString;

Console.WriteLine("Enter a word:\n");
userString = Convert.ToString(Console.ReadLine());
Console.WriteLine("\nWord is = {0}", userString);

string revString = Reverse(userString);
Console.WriteLine("Its reverse is = {0}", revString);

if (userString == revString)
Console.WriteLine("\nWord is a palindrome.\n");
else
Console.WriteLine("\nWord is not a palindrome.\n");

Console.ReadLine();
}

public static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}


Output


Notes

Some further research for you…measuring the efficiency of different reverse methods.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public static string ReverseUsingArrayClass(string text)
{
char[] chars = text.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}

public static string ReverseUsingCharacterBuffer(string text)
{
char[] charArray = new char[text.Length];
int inputStrLength = text.Length - 1;
for (int idx = 0; idx <= inputStrLength; idx++) { charArray[idx] = text[inputStrLength - idx]; } return new string(charArray); } public static string ReverseUsingStringBuilder(string text) { if (string.IsNullOrEmpty(text)) { return text; } StringBuilder builder = new StringBuilder(text.Length); for (int i = text.Length - 1; i >= 0; i--)
{
builder.Append(text[i]);
}

return builder.ToString();
}

private static string ReverseUsingStack(string input)
{
Stack<char> resultStack = new Stack<char>();
foreach (char c in input)
{
resultStack.Push(c);
}

StringBuilder sb = new StringBuilder();
while (resultStack.Count > 0)
{
sb.Append(resultStack.Pop());
}
return sb.ToString();
}

public static string ReverseUsingXOR(string text)
{
char[] charArray = text.ToCharArray();
int length = text.Length - 1;
for (int i = 0; i < length; i++, length--)
{
charArray[i] ^= charArray[length];
charArray[length] ^= charArray[i];
charArray[i] ^= charArray[length];
}

return new string(charArray);
}

static void Main(string[] args)
{
int cycleCount = 20;

string testString = string.Join(";", new string[] {
new string('l', 10),
new string('e', 10),
new string('v', 10),
new string('e', 10),
new string('l', 10),
});

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

for (int i = 0; i < cycleCount; i++)
{
Console.WriteLine(ReverseUsingCharacterBuffer(testString));
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingCharacterBuffer: " + stopwatch.ElapsedMilliseconds + "ms\n");

stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
Console.WriteLine(ReverseUsingArrayClass(testString));
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingArrayClass: " + stopwatch.ElapsedMilliseconds + "ms\n");

stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
Console.WriteLine(ReverseUsingStringBuilder(testString));
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingStringBuilder: " + stopwatch.ElapsedMilliseconds + "ms\n");

stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
Console.WriteLine(ReverseUsingStack(testString));
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingStack: " + stopwatch.ElapsedMilliseconds + "ms\n");

stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
Console.WriteLine(ReverseUsingXOR(testString));
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingXOR: " + stopwatch.ElapsedMilliseconds + "ms\n");

Console.ReadKey();
}
}
}