(* Program: PALIN.PAS Author : Kim Moser Date : 29 April, 1992 System : IBM PC / Borland Turbo Pascal 5.5 Descrip: Tests strings for "palindrome-ness" Copyright (c) 1992 Kim Moser All rights reserved *) program palin(input, output); function upper(ch: char): char; begin if (ch in ['a'..'z']) then upper := chr(ord(ch) - ord('a') + ord('A')) else upper := ch; end; function is_punct(ch: char): boolean; begin is_punct := NOT (ch in ['A'..'Z', 'a'..'z', '0'..'9']); end; function is_palindrome(s: string): boolean; var i, j: integer; r: boolean; begin r := true; (* Assume *) i := 1; j := length(s); while (i <= j) do begin (* Skip punctuation: *) while (is_punct(s[i])) do i := i + 1; while (is_punct(s[j])) do j := j - 1; (* If end not reached, test characters: *) if (i <= j) then begin if (upper(s[i]) <> upper(s[j])) then begin r := false; (* Not a palindrome *) i := j + 1; (* Break out of loop *) end; end; (* Next character... *) i := i + 1; j := j - 1; end; is_palindrome := r; end; procedure try(s: string); begin write('"', s, '" is '); if (NOT is_palindrome(s)) then write('NOT '); writeln('a palindrome.'); end; begin (* main *) try('Madam, I''m Adam'); try('Madam, I''m Noah'); try('Madam, in Eden I''m Adam'); try('Able was I, ere I saw Elba'); try('A man, a plan, a canal: Panama!'); try('A man, a plan, a canal: Suez!'); try('Step on no pets'); try('Sit on a potato pan, Otis'); try('Sit on a bedpan, Otis'); try('Rats live on no evil star'); try('Mice live on no evil star'); try('Nair a fat Sartre, pert Rastafarian'); try('Doc, note, I dissent: a fast never prevents a fatness; I diet on cod'); end.