Sunday, August 2, 2009

I need to write a C++ program that determines if a string is a palindrome?

i need this program for a assignment

I need to write a C++ program that determines if a string is a palindrome?
Set up a for loop starting at the length of the string and moving backward one character at a time until you reach the first character.





Inside the loop copy the character you are at to the end of a new string which you dimensioned the same size as the original .





When you reach the end of the first string all of the characters should be copied in reverse order to the second string.





Now just compare your original string to the new string if they are equal then the original sting was a palindrome.
Reply:This should not be too bad. I don't know c++ at all, but you should be able to loop on the number of letters in the string. Here is some pseudo-code





if (n is odd) // n is the length of the string


{lengthToCheck = (n-1)/2}


else


{lengthToCheck = n/2}


endif





palindrome = true


for i= 1 to lengthToCheck


{if(i-th element in string %26lt;%26gt; n+1-i-th element in string) then


{palindrome = false


exit(false)


}


}





That should do it. Hope this is helpful from someone who knows very little C++ code.


No comments:

Post a Comment