Code Examples

Is palindrome

programming

A palindrome is a word, number, phrase, or other sequence of symbols that reads the same backwards as forwards.

The following function, in Python language, checks if a string passed in parameters is a palindrome, and returns True or False, depending on the case.

def is_palindrome(input_string):
	# We'll create two strings, to compare them
	new_string = ""
	reverse_string = ""
	# Traverse through each letter of the input string
	for letter in input_string:
		# Add any non-blank letters to the 
		# end of one string, and to the front
		# of the other string. 
		letter = letter.lower()
		if letter !=" ":
			new_string = new_string + letter 
			reverse_string = letter + reverse_string
	# Compare the strings
	if new_string == reverse_string:
		return True
	return False

# Test cases
print(is_palindrome("madam")) # Must return True
print(is_palindrome("radar")) # Must return True
print(is_palindrome("home")) # Must return False