hello, pls cn anyone give a well-explained user-defined function that can check if a string is a palindrome?
Thanks.

Recommended Answers

All 3 Replies

Hi benjamin 8 welcome to DaniWeb.
What is there to explain?
Strictly speaking C# uses methods, not functions.
If you know how to reverse a string and how to compare a string, this is easy.
If you have any trouble, please show us your code and point out the difficulties you are having.

Thank you, Dani.
Well, I am a beginner.
In some other languages, you could create a function and then call it in your main program.
We are not allowed to use built-in functions, and there lies my problem. Any ideas with what else I could do?

Hi Benjamin! You could impress your teacher with this (needs using System.Linq at the start):

public bool isPalindrome(string aString)
{
return aString.Contains(new string(aString.Reverse<char>().ToArray<char>()));
}

Seriously. A string is an array of characters. Loop that array (i.e. string) by comparing first and last characters, then second character and second last character. As long as they are the same, you have a palindrome. When you encounter a mismatch, the string is not a palindrome.

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.