# Algorithm and Flowchart to check whether a string is Palindrome or not

[135729 views]

## What is palindrome string?

A string is said to be a palindrome when the string read from left to right is equal to the string read from right to left, ignoring the differences between uppercase and lowercase characters.

In simple words, a string is said to be palindrome when the string read backwards is equal to the given string or when both the string and its reverse are equal.

Now let’s take a look at the algorithm and flowchart to check whether a given string is palindrome or not, for better understanding.

### Algorithm to check whether a string is palindrome or not:

Step 1. Start Step 2. Read the string from the user Step 3. Calculate the length of the string Step 4. Initialize rev = “ ” [empty string] Step 5. Initialize i = length - 1 Step 6. Repeat until i>=0: 6.1: rev = rev + Character at position ‘i’ of the string 6.2: i = i – 1 Step 7. If string = rev: 7.1: Print “Given string is palindrome” Step 8. Else: 8.1: Print “Given string is not palindrome” Step 9. Stop

### Explanation:

We are starting the algorithm by taking the string to be checked as input from the user. After that, the length of the string is calculated and stored in a variable, say ‘length’. To check whether a string is palindrome or not, the given string must be reversed. To store the reversed string, we are initializing a variable ‘rev’ as an empty string. That being done, we are starting a loop with initial value i = length – 1. In this loop, we are reversing the string, one character at a time by performing: rev = rev + character at position i. Here, the ‘+’ operator performs the concatenation of the characters of the string in reverse order. After that, the value of ‘i’ is decremented by 1. This loop runs until i >= 0. Once this loop ends, we have the reversed string in the variable rev.

We will now check whether both the strings are equal or not, ignoring the cases of the characters. If both the strings are equal, the given string is palindrome, else, the given string is not palindrome.