In: Computer Science
Foundation of Computer Science
A palindrome is a string that reads the same forward and backward.
1. Describe an algorithm that determines whether a string of n characters is a palindrome.
2. Write its corresponding program using your favorite programming language.
Algorithm
Step1-Start
Step2-Input a string and store it in a variable (eg-ostring)
Step3- Reverse the string and store it in another variable (eg-rstring)
Step4-Compare the original string and reversed string(ie ostring and rstring), If both are equal return true(string is pallindrome) else return false(string is not pallindrome)
Step5-Stop
Program(Python Language)
def check_pal(ostring): #pallindrome function with argument ostring which is input string
rstring="" #variable rstring(for storing reverse string) with empty string initialized
for l in ostring: #loop through input string character by character
rstring=l+rstring #add the characters to rstring from right to left(ie in reverse)
if(rstring==ostring): #compare input string and reverse string
print("String is a pallindrome ") #if true it is a pallindrome
else: #if input string not equal to reverse(ie false)
print("String is not a pallindrome") #print it is not a pallindrome
while(1): #keep program repeating till user want
ostring=input("Enter a string:") #input string and store in ostring variable
check_pal(ostring)
#call the function with input as argument to check if string is pallindrome
#please use tab for indentation as python language is used(as given in the image)
Output