Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,006 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,361 people online right now. Registration is fast and FREE... Join Now!




palindrome

 
Reply to this topicStart new topic

palindrome

ladiesman
31 Mar, 2008 - 08:39 PM
Post #1

New D.I.C Head
*

Joined: 9 Feb, 2008
Posts: 35

i need help with wrtiting a palindrome program but i want to know how these palindrome works. alsowhat all does the code entail dont write it for me just list like for example "capital or small words". thank you.
User is offlineProfile CardPM
+Quote Post

skaoth
RE: Palindrome
31 Mar, 2008 - 08:54 PM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 342



Thanked: 10 times
Dream Kudos: 100
My Contributions
The wikipedia says:

"A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either direction (the adjustment of punctuation and spaces between words is generally permitted)."

Essentially is the string your looking at spelled the same forwards and backwards


User is online!Profile CardPM
+Quote Post

binnu
RE: Palindrome
31 Mar, 2008 - 09:36 PM
Post #3

New D.I.C Head
*

Joined: 30 Mar, 2008
Posts: 5

QUOTE(ladiesman @ 31 Mar, 2008 - 09:39 PM) *

i need help with wrtiting a palindrome program but i want to know how these palindrome works. alsowhat all does the code entail dont write it for me just list like for example "capital or small words". thank you.




for example 131 is pallindrome
suppose for the asked value of n,131 is entered
intially u have to assign the n value into another variable say 'c',bcoz after completion of loop the value of n will be changed.by doin this a holds the value of n.And also assign zero to 'b'(b=0;)
first u have 2 check whether the given number is = 0 or not if not (while(n!=0), then enter the loop.
{
(1)then find remainder of the number with 10(a=n%10)it will extract the last digit in the number.i.e '1' which is stored in a.

(2)b=(b*10)+a;(b is initialized with '0')
here the value of is,b=(0*10)+1=1
(3)now find the modulus value of the number n i.e 131
n=n/10;
it will gives u the value 13 and stored in n itself.
}the loop is repeated second time ,then the digit 3 will be extracted from the number 13,and the value 'b' becomes (b=(1*10)+3))=13,again the modulus of n i.e 13 is 1.the value of n becomes 1.it enters the loop again
a value becomes 1.b value becomes(b=(13*10)+1))=131.

THEN compare the value of b with c(if b==c)
then
is pallin
else
not pallin smile.gif
User is offlineProfile CardPM
+Quote Post

syazhani
RE: Palindrome
31 Mar, 2008 - 09:56 PM
Post #4

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 33


My Contributions
QUOTE(binnu @ 31 Mar, 2008 - 10:36 PM) *

QUOTE(ladiesman @ 31 Mar, 2008 - 09:39 PM) *

i need help with wrtiting a palindrome program but i want to know how these palindrome works. alsowhat all does the code entail dont write it for me just list like for example "capital or small words". thank you.




for example 131 is pallindrome
suppose for the asked value of n,131 is entered
intially u have to assign the n value into another variable say 'c',bcoz after completion of loop the value of n will be changed.by doin this a holds the value of n.And also assign zero to 'b'(b=0;)
first u have 2 check whether the given number is = 0 or not if not (while(n!=0), then enter the loop.
{
(1)then find remainder of the number with 10(a=n%10)it will extract the last digit in the number.i.e '1' which is stored in a.

(2)b=(b*10)+a;(b is initialized with '0')
here the value of is,b=(0*10)+1=1
(3)now find the modulus value of the number n i.e 131
n=n/10;
it will gives u the value 13 and stored in n itself.
}the loop is repeated second time ,then the digit 3 will be extracted from the number 13,and the value 'b' becomes (b=(1*10)+3))=13,again the modulus of n i.e 13 is 1.the value of n becomes 1.it enters the loop again
a value becomes 1.b value becomes(b=(13*10)+1))=131.

THEN compare the value of b with c(if b==c)
then
is pallin
else
not pallin smile.gif

Are you serious? blink.gif

Ok basically, a string is palindrome if you read it normally is the same as reading it backwards. Examples are available here: http://www.palindromelist.com/

Example: "Damn, I, Agassi, miss again! Mad!"
The tricky part is to ignore punctuations. So given a string, you first iterate through it's characters and copy to another string only if it is alphabets / numeric (using isalpha(char) function).

Example: "DamnIAgassimissagainMad"
Now you have a new version of the string, without spaces nor punctuations. But there is still one problem. Lower/Upper case. So re-iterate (or do it while you were copying in the step above) and use tolower(char) function to convert to lower case (it'll stay lower case if you pass an already lower case letter).

Example: "damniagassimissagainmad"
Finally, compare both ends of the string, moving inwards. You will want to loop for n = string.length() / 2 (because you're only reiterating half the size -- to the middle of the string).
Let i be the index for the left side, j be the index for the right side.
i = 0
j = string.length() - 1

every iteration of n times, increment i, decrement j, compare. Once they are not the same, immediately exit as we know it's not palindrome.

Else set a proper boolean then output that the string is palindrome.

Edit: Refer here for proper isxxxx functions you might need: http://www.cplusplus.com/reference/clibrary/cctype/
Once you've got the solution and feeling up for it, checkout functions from <algorithm>, comparing for palindrome after case conversion and stripped punctuations will be a one-liner.

This post has been edited by syazhani: 31 Mar, 2008 - 09:59 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 12:51PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month