TCL (Tool Command Language) Question:

Download Job Interview Questions and Answers PDF

How to check whether a string is palindrome or not using TCL script?

TCL Interview Question
TCL Interview Question

Answers:

Answer #1
Code for the above pseudo code.Check if it works!!!!!

gets stdin a
set len [ string length $a ]
set n [ expr $len/2 ]

for { set i 0 } { $i < $n } { incr i 1 } {
set b [ string index $a $i ]
set c [ expr $len - 1 - $i ]
set d [ string index $a $c ]

if {$b != $d} {
puts "not a palindrome"
exit

}

}
puts "Palindrome"

Answer #2
#!/usr/bin/tclsh
puts "Enter the inital string \n"
set istring [gets stdin]
set splstring [split $istring ""]
set strlen [llength $splstring]
puts $strlen
set error 0
for {set i 0} {$i <= [llength $splstring]} {incr i} {
set strlen [expr $strlen - 1]
if {[lindex $splstring $i] == [lindex $splstring $strlen]} {
} else {
puts "[lindex $splstring $i] != [lindex $splstring $strlen]"
set error [expr $error + 1]
}
}
if {$error != 0 } {
puts "Given string is not palindrome"
} else {
puts "Given string is palindrome"
}

Download TCL Interview Questions And Answers PDF

Previous QuestionNext Question
How do you find the length of a string without using string length command in TCL?Set ip address as 10.30.20.1 write a script to replace the 30 with 40?