; ========================================================= ; Subroutine to check for a palindrome ; ; d0.w = length of string minus 1 ; a0.l = starting RAM address of string (must be 8 bits per character) ; returns with ZERO flag set if the string is a palindrome, clear otherwise ; ========================================================= CheckPalin: andi.l #$0000FFFF,d0 ;allow using d0 as long tst.w d0 ;is string 1 character long? beq.b CheckPalin_Return ;if so, return with ZERO flag set movea.l a0,a1 adda.l d0,a1 ;set a1 to address of last character subi.w #1,d0 ;get whole length lsr.w #1,d0 ;get half the length CheckPalin_Loop: move.b (a0),d1 ;get character from start of string cmp.b (a1),d1 ;compare with character from end of string bne.b CheckPalin_Return ;if unequal, return with ZERO flag unset adda.l #1,a0 ;increase offset from start of string suba.l #1,a1 ;decrease offset from end of string dbf d0,CheckPalin_Loop move.w #0,d0 ;set ZERO flag CheckPalin_Return: rts
The credit for this goes to Rob (keiji.msk@googlemail.com).
I may hate some (if not most) of his coding practices, but you gotta admit, a guy who can write a palindrome checker using 4 temporary variables, in ASM, is fucking smart, despite the answer being logical as hell.
For comparison, my own Java solution (which I had to write for a class) is:
import java.util.Scanner; public class Palindrome { private static Scanner stdin = new Scanner( System.in ); private static String word = ""; public static void main( String[] args ) { instructions( ); while( question( "Enter a string" ) == true ) { if( checkPalindrome( ) ) System.out.println( "This is a palindrome" ); else System.out.println( "This is not a palindrome" ); } } private static void instructions( ) { System.out.println( "This program will analize any word to see if" ); System.out.println( "it is a palindrome or not." ); System.out.println( "To quit the program, type in 'quit'." ); } private static boolean question( String question ) { System.out.println( question ); word = stdin.nextLine( ); if( word.toLowerCase( ).equals( "quit" ) ) return false; else return true; } private static boolean checkPalindrome( ) { String reverse = reverseWord( ); if( word.toLowerCase( ).contentEquals( reverse.toLowerCase( ) ) ) return true; else return false; } private static String reverseWord( ) { String newWord = ""; LinkedStack<Character> wordStack = new LinkedStack<Character>( ); for( int i = 0; i < word.length( ); i++ ) wordStack.push( word.charAt( i ) ); int size = wordStack.size( ); for( int i = 0; i < size; i++ ) newWord += wordStack.pop( ); return newWord; } }
