C#: Check if Input is Palindrome

Mar 5, 2014
Here is one algorithm I find a challenge back on my student days.

* Check if string value entered by user is a palindrome or not.

/// <summary>
/// Checks if entered string value is a palindrome or not.
/// </summary>
/// <param name="p_word">Value entered by user</param>
/// <returns>If palindrome, returns true; otherwise, returns false</returns>
public bool IsPalindrome(string p_word)
{
 string revWord = string.Empty;
 char[] wordChar = p_word.ToCharArray();

 for (int index = p_word.Length - 1; index >= 0; index--)
 {
  revWord += wordChar[index];
 }

 if (revWord.Equals(p_word))
  return true;
 else
  return false;
}

If you have any better solution, please don't hesitate to share them on the comments section. (:

::EDIT::

Here's another solution by .NET GENE.

using System.Linq;

public bool IsPalindromeEnum(string p_word)
{
 string revWord = new string(p_word.Reverse().ToArray());

 if (revWord.Equals(p_word))
  return true;
 return false;
}

Thanks! :D

No comments:

Post a Comment