[Tcl] My Quick and Dirty Palindrome Script

I was looking at changing my managed hosting provider to DreamHost and saw they were hiring for Systems Administrators. To apply for the position, they require one to write a Palindrome script in Perl. Being the non-Perl wizard that I am, I decided to write it in Tcl instead. It took me about a good 15 minutes to think it through and produce a working version that passes all the palindromes I could throw at it. Here is what it looks like:

—-cut—-

#! /bin/sh
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# the next line restarts using tclsh \
exec tclsh8.4 “$0″ ${1+”$@”}

# =======================================================
proc IsPalindrome { theString } {
set theString [string trim [string tolower $theString]]
for {set i 0} {$i<[string length $theString]} {incr i} {
if { [string compare "[string index $theString end-$i]" "[string index $theString $i]"] != 0 } {
puts "false"; exit 2
} else { puts "true"; exit 0 }
}
}

IsPalindrome [lindex $argv 0]

—-cut—-

I am sure there are much better Tcl palindrome scripts than this. If you have one post it!:)

Cheers,
-swinful

This entry was posted in *Nix. Bookmark the permalink.

Leave a comment