Skip to content

Instantly share code, notes, and snippets.

@henrik-ch
Created January 18, 2011 20:04
Show Gist options
  • Save henrik-ch/785037 to your computer and use it in GitHub Desktop.
Save henrik-ch/785037 to your computer and use it in GitHub Desktop.
for Euler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EulerPalindrome {
class Program {
static void Main(string[] args) {
string test2 = "Anna";
Console.WriteLine(test2.RightIndex(1));
Console.WriteLine(test2.IsPalindrome());
}
}
static class Extensions
{
public static bool IsPalindrome(this string input)
{
input = input.ToLower();
int numberOfComparisons = input.Length/2;
numberOfComparisons.Dump();
if (numberOfComparisons < 1)
return false;
bool retVal = true;
for (int numCompare = 1; numCompare <= numberOfComparisons; numCompare++)
{
input.LeftIndex(numCompare).Dump();
input.RightIndex(numCompare).Dump();
if (input.LeftIndex(numCompare) != input.RightIndex(numCompare))
retVal = false;
}
return retVal;
}
public static string LeftIndex(this string input, int numIndex)
{
return input.Substring(numIndex, 1);
}
public static string RightIndex(this string input, int numIndex)
{
return input.Substring(input.Length - numIndex, 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment