Skip to content

Instantly share code, notes, and snippets.

@bradjohansen
Created April 30, 2010 01:23
Show Gist options
  • Save bradjohansen/384565 to your computer and use it in GitHub Desktop.
Save bradjohansen/384565 to your computer and use it in GitHub Desktop.
A simple Palindrome Tester written in C#
using System;
using System.Text.RegularExpressions;
namespace Palindrome
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Let's test for palindromes!\n");
Console.WriteLine("Please enter a word.");
// User Input
string input = Console.ReadLine();
// Check to see if input is correct
// If not, clear console.
Console.WriteLine("Is this your word? (y/n)");
Console.Write("{0}: ", input);
string yes_no = Console.ReadLine();
if (yes_no == "y" || yes_no == "yes")
{
Console.WriteLine("Calculating Palindromism... \n");
Palindrome.Tester(input);
}
else
{
Console.WriteLine("Trying again... \n");
Main(args); // Restart The Program
}
Console.ReadLine();
}
}
class Palindrome
{
/// <summary>
/// Gets an input from user and checks to see if it is a palindrome.
/// </summary>
public static void Tester(string input)
{
string pattern = "\\W";
string data = Regex.Replace(input.ToLower() , pattern, String.Empty);
if (data == StringHelper.ReverseString(data))
{
Console.WriteLine("{0} is a Palindrome.", input);
}
else
{
Console.WriteLine("{0} isn't a Palindrome.", input);
}
}
}
static class StringHelper
{
/// <summary>
/// Receives string and returns the string with its letters reversed.
/// </summary>
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
}
@bradjohansen
Copy link
Author

Made changes to where it outputs the original input, and not the stripped version.

@bradjohansen
Copy link
Author

Fixed the if statement to where it now restarts the program if the user puts in "n", "no", or anything but yes.

@Testo12
Copy link

Testo12 commented Sep 6, 2018

8 years later. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment