C# Programming > Data

C# Palindrome Function

Check Palindrome String

A popular exercise for programmers is to check for palindromes. A C# palindrome checking function (for strings) will test whether a string can be written the exact same way forwards and backwards. That is the definition of a Palindrome.

C# String Processing Library PRO

We will write two different C# palindrome functions: a simple one that is very easy to understand and a slightly less readable function that will be faster and more efficient. For the sake of understanding, neither will use recursion. If you are doing a programming assignment with Palindromes, I recommend you read the article rather than copy the source code.

Easy Palindrome Function

The simplest concept to test a string as a palindrome is to check if it's the same reversed as it was originally. Thus our first C# palindrome function will reverse the entire string and compare it to the original.

string reversedString = "";
for (int i = inputString.Length - 1; i >= 0; i--)
{
     reversedString += inputString[i];
}

To reverse a string in C# use a for-loop that transverses the string from right to left, copying each character over to a new string, writing the characters from left to right.

Once the string is reversed, the strings are easy compared:

return inputString == reversedString;

Hard Palindrome Function

The limitation of the easy C# Palindrome function as you can probably tell, is that it first has to reverse the entire string before it can come to a decision.

Short strings such as "kajak" and "noon" will not make a difference, but say you wanted to efficiently check a phrase to be a palindrome?

From a speed stand point, the easy function has no chance of faster processing speeds for strings that are clearly not palindromes. From a memory stand point, the entire string must be duplicated, doubling the cost of memory.

Thus our more efficient C# palindrome checker will not use any helper strings. In fact, it does not need to loop the entire string, the for-loop only has to go half-way:

for (int i = 0; i < inputString.Length / 2; i++)

(Quick Note: Length is an integer, meaning that a string with length 5 will go up to index 2. This is perfect since characters that are exactly in the middle of a string--think "kajak"--make no difference.)

Think of it as a mirror, each character in the first half of the string has to be compared to the matching character in the other half of the string. So for example, the first character must match the last character, the second one match the second to last, etc.

Getting the characters in the first half should be easy enough, as for getting the matching character, use the index:

inputString.Length - i - 1

Where i is the current index.

Finally, if you think about it, the C# palindrome function only has to find a single pair of characters that don't match to declare the entire string not a palindrome. Thus the inside of the for-loop will look only like this:

if (inputString[i] != inputString[inputString.Length - i - 1])
    return false;

If the string reaches the end of the loop without finding a mismatch, then the C# palindrome function has officially confirmed the string to be a palindrome.

Check out the Visual C# Kicks String Function Library for more super-fast text functions.

Back to C# Article List