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

Edited by benjamin_8: Not well- explained

4 Years
Discussion Span
Last Post by Teme64

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.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.