C# programmingJJoy19yAsked Sep 20, 2007, 4:36 AM3.8kWhat is the code to check whether a string is a palindrome?
AAlan19y agoPosted Sep 20, 2007, 3:47 PMThe code in Mahesh's link deals with a numeric palindrome. The following code deals with a string palindrome: using System; class StringPalindrome{ static void Main() { string[] sa = new string[]{"rotor", "caravan", "level", "palindrome"}; foreach (string s in sa) { char[] ca = (char[])s.ToCharArray().Clone(); Array.Reverse(ca); string rev = new string(ca); string temp = (s == rev) ? "is" : "is not"; Console.WriteLine("\"{0}\" {1} a palindrome", s, temp); } Console.ReadLine(); }}
Mahesh Chand19y agoPosted Sep 20, 2007, 8:04 AMHere are some samples including palindrome. http://www.c-sharpcorner.com/UploadFile/nrsurapaneni/SortSeachNRS11242005061300AM/SortSeachNRS.aspx
JMJan Montano19y agoPosted Sep 20, 2007, 6:39 AMHi Joy, The code for the palindrome is not that hard. Have you tried to actually coding it yourself? How far have you been with it? Cheers, Jan
AlanPosted Sep 20, 2007, 3:47 PM
The code in Mahesh's link deals with a numeric palindrome. The following code deals with a string palindrome:
using System;
class StringPalindrome
{
static void Main()
{
string[] sa = new string[]{"rotor", "caravan", "level", "palindrome"};
foreach (string s in sa)
{
char[] ca = (char[])s.ToCharArray().Clone();
Array.Reverse(ca);
string rev = new string(ca);
string temp = (s == rev) ? "is" : "is not";
Console.WriteLine("\"{0}\" {1} a palindrome", s, temp);
}
Console.ReadLine();
}
}
Mahesh ChandPosted Sep 20, 2007, 8:04 AM
Here are some samples including palindrome.
http://www.c-sharpcorner.com/UploadFile/nrsurapaneni/SortSeachNRS11242005061300AM/SortSeachNRS.aspx
Jan MontanoPosted Sep 20, 2007, 6:39 AM
The code for the palindrome is not that hard. Have you tried to actually coding it yourself? How far have you been with it?
Cheers,
Jan