I have an assignment in which I have to write a palindrome program. I searched online for some advice, but I couldn't find any without the codes using the def function. I'm only allowed to use for and while loops, if, elif, and else statements. I have the program ask the users to input a word or phrase. In order to have it recognize a palindrome, I tell the computer that position 0 and position -1 of the word have to be the same.
In addition, the program also has to look for palindromes with a word or phrase so there's some indexing going on as well. For example, radar also has the palindrome "ada" in it as well. For all the palindromes it finds, it has to print the starting and ending position of that palindrome.
Here is what I have so far:
So if we tested, for example, "attadatta," it gets some of the palindromes but misses some others. I don't know how to fix these bugs. If anyone could lend a hand and tell me what I'm doing wrong, I would really appreciate it. Thanks everyone!
In addition, the program also has to look for palindromes with a word or phrase so there's some indexing going on as well. For example, radar also has the palindrome "ada" in it as well. For all the palindromes it finds, it has to print the starting and ending position of that palindrome.
Here is what I have so far:
PHP:
string = raaw_input("Please enter a word or phrase: ")
string_len = len(string)
print "The length of the string is ", string_len
#Here is the part where the program looks for palindromes.
for i in range(string_len):
for j in range(stringlen-1, i+1, -1):
k = 0
while (string[i+k] == string[j-k]):
k = k + 1
if (j-k < 0) or (i+k == string_len):
break
if k > 2:
print "[",i,"]", "[", i+k-1, "]\t", string[i:i+k]
So if we tested, for example, "attadatta," it gets some of the palindromes but misses some others. I don't know how to fix these bugs. If anyone could lend a hand and tell me what I'm doing wrong, I would really appreciate it. Thanks everyone!