C Program to find whether an input is a palindrome or not.

Hey there!

Welcome to ClearUrDoubt.com.

In this post, we will look at a C program to find whether a given input is a palindrome or not.

Palindrome: A palindrome is a word, number, or another sequence of characters which reads the same backward as forward.

Eg.

MADAM
RACECAR
12321

To find whether an input is a palindrome or not, we need to apply the logic as below:

Logic is to compare the first half of the string with the reverse of the second half of the string.

Eg. 1:
Input: “FLASHHSALF”
Length of the input = 10.
Divide the input into two halves. First Half: “FLASH” and Second Half: “HSALF”.
Compare the first character of First Half(i.e., “F”) with the last character of Second Half(i.e., “F”).
if they are equal, then proceed further and compare the second character in First Half(i.e., “L”) with the second last charcter in Second Half(i.e., “L”).
if they are equal, proceed further and continue the same process until all the characters in First Half/Second Half are done.

Eg. 2:
Input: “1234321”
Length of the input: 7.
If the length of the string is odd, then divide the input into two halves without considering the middle character. First Half: “123” and Second Half: “321”.

Here is the C program:

 

Output:

 

 

 

 

 

 

 

 

 

 

 

 

 

Happy Learning! 🙂

Please leave a reply in case of any queries.

Leave a Reply

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