Author Topic: Python - Check Palindrome using while loop  (Read 1849 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Python - Check Palindrome using while loop
« on: December 06, 2011, 05:51:17 pm »
Here is the python source code to check whether a given string is a palindrome or not by using while loop.

Code: (Python)
#Python Source File

#mycode.py
#returns true if the given string a palindrome, otherwise false
def IsPalindrome(inpstr):
	count = len(inpstr)
	palindrome = True
	i = 0
	while(i < count / 2 + 1):
		if(inpstr[i] != inpstr[count - i - 1]):
			palindrome = False
			break
		i = i + 1
	return palindrome

#end of function

Here is how you import and execute the code

>>>import mycode
>>> mycode.IsPalindrome("Kathir")
False
>>> mycode.IsPalindrome("madam")
True
>>>quit()

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1