Palindrome string program in C

Problem

Write a C++ algorithm that takes a string as input then check if the input string is Palindrome. A Palindrome string reads the same from left to right and from right to left as in the following example: Rats live on no evil star

Solution

We need one pass through the characters of the string to get the string length or use a built in function to get that. Once the length of the string is found then one loop can be used to compare the first character with the last character and the second character with the one before the last character and so on until the string is entirely checked. If we can find only one occurrence where two compared characters are not the same ignoring the case then the string is not Palindrome. The number of iterations needed is half the string length. If the string has an odd number of characters then the character in the middle of the string is not going to be compared with any other character.

Example code

Here is a simple C++ program code to do that

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *