How do you check whether a string is palindrome or not using TCL script?
Answer Posted / swinful
Let me know what you think. The above solutions are just as good, but this one is short and simple. It is pretty much self explanatory. Hope you like:)
--- cut----
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----
You can put the above into a script and run it, supplying a command-line argument.
Cheers
-swinful
Is This Answer Correct ? | 2 Yes | 3 No |
Post New Answer View All Answers