This is a fast and readable implementation for checking if a string is a palondrome or a near-palindrome1 in MIPS Assembly.
[1] A string is a near-palindrome if removing one character makes it a palindrome.
Please enter a string: aibohphobia
That string is a palindrome :)
Please enter a string: emre
That string is a near palindrome :|
Please enter a string: palindrome
That string is not a palindrome :(
Here is what the algorithm looks like in executable pseudo code:
string = raw_input('Please enter a string: ')
last_pos = -1
for char in string:
last_pos = last_pos + 1
left_ptr = 0
right_ptr = last_pos
while (left_ptr < right_ptr):
if (string[left_ptr] != string[right_ptr]):
break
left_ptr = left_ptr + 1
right_ptr = right_ptr - 1
if (left_ptr >= right_ptr):
print 'That string is a palindrome :)'
exit()
for omit in range(last_pos + 1):
left_ptr = 0
right_ptr = last_pos
while (left_ptr < right_ptr):
if (omit == left_ptr):
left_ptr = left_ptr + 1
if (omit == right_ptr):
right_ptr = right_ptr - 1
if (string[left_ptr] != string[right_ptr]):
break
left_ptr = left_ptr + 1
right_ptr = right_ptr - 1
if (left_ptr >= right_ptr):
print 'That string is a near palindrome :|'
exit()
print 'That string is not a palindrome :('
exit()
Copyright © 2000 Emrehan Tüzün
This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.