Algorithm


  1. Input: Accept a string from the user.

  2. 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 it end).

  3. Loop Until Pointers Meet:

    While start is less than end, repeat the following steps.
  4. Compare Characters:Check if the character at position start is equal to the character at position end.
  5. Update Pointers:

    • If the characters are equal, increment start and decrement end.
    • If the characters are not equal, the string is not a palindrome.
  6. Palindrome Check:

    • If the loop completes without finding unequal characters, then the string is a palindrome.
  7. 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

x
+
cmd
civic is Palindrome

#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

x
+
cmd
civic is Palindrome

#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

x
+
cmd
civic is Palindrome

#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

x
+
cmd
civic is: Palindrome

#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

x
+
cmd
civic is : Palindrome
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

x
+
cmd
civic is : Palindrome

#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

x
+
cmd
civic is : Palindrome
Advertisements

Demonstration


Python Programing Example to Check Whether a String is Palindrome or Not-DevsEnv