string - Checking palindrome word in python -
i have code check whether word palindrome or not:
str = input("enter string") l = len(str) p = l-1 index = 0 while index < p: if str[index] == str[p]: index = index + 1 p = p-1 print("string palindrome") break else: print("string not palindrome")
if word inputted, example : rotor , want program check whether word palindrome , give output "the given word palindrome".
but i'm facing problem that, program checks first r , r , prints "the given word palindrome" , checks o , o , prints "the given word palindrome". prints result many times checking word.
i want result delivered once. how change code?
i had make few changes in code replicate output said saw.
ultimately, want message displayed @ end of comparisons. in case, had inside loop, each time loop ran , hit if
condition, status message printed. instead, have changed prints when 2 pointers index
, p
@ middle of word.
str = input("enter string: ") l = len(str) p = l-1 index = 0 while index < p: if str[index] == str[p]: index = index + 1 p = p-1 if index == p or index + 1 == p: print("string palindrome") else: print("string not palindrome") break
wiki
Comments
Post a Comment