testing a word for palindromness(heh)

Pex

Banned
Aug 21, 2003
1,161
0
0
in c++ would it just be a test between each element in a string? or is there a more obvious way?
 

Drakkon

Diamond Member
Aug 14, 2001
8,401
1
0
that or make a copy...reverse the copy...then compare the two strings...
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: BingBongWongFooey
python:

if string == string[::-1]:
  print "this is a palindrome"

no way in hell you can say that "string[::-1]" is syntactically simpler than "reverse $string" :p
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,005
126
Reverse the string and then do a strcmp to see if they match. I think string reversing exists in the string class but I'm not 100% sure.

Failing that, you could use two pointers, on at each end of the string.
Each time the two pointers match, move each of them closer to the centre.
If they both reach the centre or they pass each other, the string is a palindrome.
If they don't match before reaching the centre, it's not a palindrome.