Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
string s,revs="";
            Console.WriteLine(" Enter string");
            s = Console.ReadLine();
            for (int i = s.Length-1; i >=0; i--) //String Reverse
            {
                revs += s[i].ToString();
            }
            if (revs == s) // Checking whether string is palindrome or not
            {
Posted
Updated 4-Dec-15 1:29am
v3
Comments
Patrice T 4-Dec-15 7:25am    
What is the question ?
Member 12185899 4-Dec-15 7:30am    
we should not use reverse.string()
Patrice T 4-Dec-15 7:38am    
This is not a question.
Member 12185899 4-Dec-15 8:05am    
check whether a string is paliandrome ot not
reverse the string is same as actual
EXAMPLE:madam
note:we should not use string.reverse()

well I would do it like this:

C#
string s = "racecar";
bool b = s == s.Reverse().ToString();


but as you said not to use reverse:

C#
string s = "racecar";
for (int j = 0; j < (s.Length / 2) - s.Length % 2; j++)
{
  if (s[j] != s[s.Length - 1 - j])
    return false;
}
return true;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Dec-15 10:54am    
5ed, see my comment to Solution 2.
—SA
Instead hand-crafting a reverse method, you could iterate over the characters to find out if the string is palindrome or not.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Dec-15 10:54am    
It's already done in solution 1, but the time is the same, so a 5. :-)
The wise point here is: this iteration and checkup is much cheaper (no matter if it is important factor or not) then even calling existing string.Reverse. It would create a new string object, which we don't need.
—SA
Andy Lanng 4-Dec-15 12:11pm    
agreed - I would still use reverse because I'm just lazy like that. Also, performance isn't often an issue I need to worry about.

PS: I also think solution 3 made a good point regarding special chars (although the execution looks a little broken)
Sergey Alexandrovich Kryukov 4-Dec-15 12:56pm    
Not at all — it was you who demonstrated how to work without creating a new string. Special char? — there is no such thing. It depends on what sense of the words "palindrome" is required to be applied, but you solution would work on letters after a minimal modification.
—SA
CPallini 4-Dec-15 15:21pm    
Thank you.
Further to solution 1 ...

The phrase "A dog! A panic in a pagoda!" is deemed to be a palindrome (according to this list[^]) but solution 1 will not agree because of the whitespace, punctuation marks and capitalisation

So an alternative solution is to remove all whitespace and non-character or digit characters from the source text and ensure it's all the same case e.g.
C#
private static bool IsPalindrome(string sourceText)
{
    var testChars = new string((from c in sourceText.ToLower()
                                where char.IsLetterOrDigit(c)
                                select c).ToArray());

    for (var i = 0; i <= (testChars.Length % 2); i++)
        if (testChars[i] != testChars[testChars.Length - i - 1]) return false;

    return true;
}
 
Share this answer
 
Comments
Matt T Heffron 4-Dec-15 12:32pm    
+5But a challenge:
Can it be done without preprocessing the string? ;-)
CHill60 8-Dec-15 9:43am    
Interesting - the case problem can obviously be moved to the if-statement. I'll have a little think about the IsLetterOrDigit ... I'd have to have anther counter of course for the back end. Might have a play with that if I get time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900