Algorithm
-
Input: Accept a string from the user.
-
Initialize Pointers: Set two pointers, one at the beginning of the string (let's call it
start
) and the other at the end of the string (let's call itend
). -
Loop Until Pointers Meet:
Whilestart
is less thanend
, repeat the following steps. - Compare Characters:Check if the character at position
start
is equal to the character at positionend
. -
Update Pointers:
- If the characters are equal, increment
start
and decrementend
. - If the characters are not equal, the string is not a palindrome.
- If the characters are equal, increment
-
Palindrome Check:
- If the loop completes without finding unequal characters, then the string is a palindrome.
-
Output Result:
- Print whether the string is a palindrome or not based on the result of the check.
Code Examples
#1 Code Example- This exampleuses negative slicing to generate reverse of string
Code -
Python Programming
# Write a program to check whether a string is palindrome or not in python
input_string = 'civic'
rev = input_string[::-1]
if input_string == rev:
print(rev + " is Palindrome")
else:
print(rev + " is not Palindrome")
Copy The Code &
Try With Live Editor
Output
#2 Code Example- Matching ith character from front-back
Code -
Python Programming
def isPalindrome(input_str):
# check if input_str[i] is same as input_str[len(str) - i - 1]
# for each char in string
for i in range(0, len(input_str)):
# Essentially, what is being checked isif i-th character is
# equal to i-th character from the end or not
if input_str[i] != input_str[len(input_str) - i - 1]:
return False
return True
input_str = "civic"
print("Palindrome") if isPalindrome(input_str) else print("Not Palindrome")
Copy The Code &
Try With Live Editor
Output
#3 Code Example- Matching ith character from front-back, updated
Code -
Python Programming
def isPalindrome(input_str):
# Run loop from 0 to len/2
mid = int(len(input_str) / 2)
for i in range(0, mid):
# Essentially, what is being checked isif i-th character is
# equal to i-th character from the end or not
if input_str[i] != input_str[len(input_str) - i - 1]:
return False
return True
input_str = "civic"
print("Palindrome") if isPalindrome(input_str) else print("Not Palindrome")
Copy The Code &
Try With Live Editor
Output
#4 Code Example- inbuilt function
Code -
Python Programming
# main function
string = "civic"
reverse = ''.join(reversed(string))
print(string + " is: ", end="")
print("Palindrome") if string == reverse else print("Not Palindrome")
Copy The Code &
Try With Live Editor
Output
#5 Code Example- Building reverse using arithmetic
Code -
Python Programming
input = "civic"
# this will automatically generate reverse
rev = ""
for ch in input:
rev = ch + rev
print(input + " is : ", end="")
print("Palindrome") if input == rev else print("Not Palindrome")
print("string: " + input)
print("rev: " + rev)
Copy The Code &
Try With Live Editor
Output
string: civic
rev: civic
#6 Code Example- Building reverse using flag
Code -
Python Programming
input = "civic"
j = -1
flag = 0
for char in input:
# char starts from index 0
# string[j] forces to read from end
# bcz negative index are read from end
if char != input[j]:
flag = 1
break
j = j - 1
print(input + " is : ", end="")
print("Not Palindrome") if flag else print("Palindrome")
Copy The Code &
Try With Live Editor
Output
#7 Code Example- Negative traversal in Loop
Code -
Python Programming
input = "civic"
n = len(input)
revlist = []
for i in range(n - 1, -1, -1):
revlist.append(input[i])
rev = "".join(revlist)
print(input + " is: ", end="")
if input == rev:
print("Palindrome")
else:
print("Not Palindrome")
Copy The Code &
Try With Live Editor
Output
Demonstration
Python Programing Example to Check Whether a String is Palindrome or Not-DevsEnv