// IsPalindrome.cs, ©2014 by Josh Moyer // All rights reserved. using System; public class Palindrome { public enum Result { Is, Not, Null } static Result IsPalindrome(string theString) { int strLen = theString.Length; int i; int oppIndex; char currentChar; if (strLen == 0) return Result.Null; for (i = 0 ; i < strLen / 2 ; i++) { currentChar = theString[i]; oppIndex = strLen - i ; if (currentChar == theString[oppIndex]) continue; else return Result.Not; } return Result.Is; } private struct TestCase { public readonly string TestInput; public readonly Result TestResult; public TestCase(string Input, Result Result) { TestInput = Input; TestResult = Result; } } public static void Main() { Result CurrentResult; TestCase[] TestCases = new TestCase[] { //new TestCase(null, Result.Null), new TestCase("", Result.Null), new TestCase(" ", Result.Is), new TestCase(" ", Result.Is), new TestCase(" ", Result.Is), new TestCase("A", Result.Is), new TestCase("AA", Result.Is), new TestCase("AB", Result.Not), new TestCase("AAA", Result.Is), new TestCase("AAB", Result.Not), new TestCase("ABA", Result.Is), new TestCase("radar", Result.Is), new TestCase("Radar", Result.Not), new TestCase("Rädar", Result.Not), new TestCase("1", Result.Is), new TestCase("12", Result.Not), new TestCase("123", Result.Not), new TestCase("121", Result.Is), }; foreach (TestCase CurrentCase in TestCases) { CurrentResult = IsPalindrome(CurrentCase.TestInput); if (CurrentResult == CurrentCase.TestResult) continue; else Console.WriteLine(CurrentCase.TestInput + " failed test."); } } }