SEWilson
Check me out on the web at efnetcsharp.net or at my blog.
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Gary Daniels and Evan Goldring - Mock whiteboard problem
Apr 04, 2005 at 7:11 PMnamespace Infocus
{
using System;
public sealed class Util
{
private Util(){}
/// <summary>
/// Verifies the input text is a palindrome.
/// </summary>
/// <param name="input">The text to be verified.</param>
/// <param name="ignoreWhitepspace">Option set to true to ignore whitespace.</param>
/// <param name="caseSensitive">Option that toggles case-sensitive.</param>
/// <param name="alphaOnly">Option that verifies only alpha-numeric characters exist.</param>
public static void VerifyPalindrome(string input, bool ignoreWhitespace, bool caseSensitive, bool alphaOnly)
{
#if DEBUG
// verify input is not null
// NOTE: we could simply rely on Replace() below to throw this exception, hence DEBUG
if (input == null)
throw new NullReferenceException("Input cannot be null.");
#endif
// strip whitespace
if (ignoreWhitespace)
input = System.Text.RegularExpressions.Regex.Replace(input, @"\s", "");
// verify alphanumeric
if (alphaOnly && System.Text.RegularExpressions.Regex.IsMatch(input, @"\W"))
throw new ApplicationException("Input contained non-alphanumeric characters.");
// lowercase input
if (!caseSensitive)
input = input.ToLower();
// verify input length is valid for a palindrome
if (input.Length < 2)
throw new ApplicationException("Length of input must be greater than 1.");
int forwardIndexer = 0, reverseIndexer = input.Length-1;
while ((forwardIndexer <= reverseIndexer) && (input[forwardIndexer] == input[reverseIndexer]))
{
forwardIndexer++;
reverseIndexer--;
}
if (forwardIndexer <= reverseIndexer)
throw new ApplicationException("Not a valid Palindrome.");
}
/// <summary>
/// Performs a case insensitive, non-whitespace intensive palindrome verification on the input string.
/// </summary>
/// <returns>true if input is a palindrome</returns>
public static bool IsPalindrome(string input)
{
try
{
VerifyPalindrome(input, true, false, false);
}
catch (ApplicationException appex)
{
return false;
}
return true;
}
}
}
I prefer that exceptions be leveraged especially for internal code. Optimization based on failure-rates aside, I think it's more elegant to say in-code:
string input = "dood";
VerifyPalindrome(input);
and then fail back to the caller if he supplied us with invalid input. No need to continually re-invent error checks because of booleans along the stack.
Of course, 1 or two more overloads wouldn't hurt.
I didn't read through ALL posts made by everyone. If it looks like I copied your code, sorry, just know I didn't get past the t-sql post on the first page before realizing I thought the code should be done differently. This took me 9 minutes. I didn't test it (whiteboards don't have a debugger at Microsoft, do they?)