Home » Program to Determine Whether a Given String is Palindrome

Program to Determine Whether a Given String is Palindrome

by Online Tutorials Library

Program to determine whether a given string is Palindrome

Explanation

In this program, we need to check whether a given string is palindrome or not.

A string is said to be palindrome if it reads the same backward as forward. For e.g. above string is a palindrome because if we try to read it from backward, it is same as forward. One of the approach to check this is iterate through the string till middle of string and compare a character from back and forth.

Algorithm

  1. Define a string.
  2. Define a variable flag and set it to true.
  3. Convert the string to lowercase to make the comparison case-insensitive.
  4. Now, iterate through the string forward and backward, compare one character at a time till middle of the string is reached.
  5. If any of the character doesn’t match, set the flag to false and break the loop.
  6. At the end of the loop, if flag is true, it signifies string is a palindrome.
  7. If flag is false, then string is not a palindrome.

Solution

Python

Output:

Given string is palindrome  

C

Output:

Given string is palindrome  

JAVA

Output:

Given string is palindrome  

C#

Output:

Given string is palindrome  

PHP

Output:

Given string is palindrome  

Next Topic#

You may also like