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

Answers were Sorted based on User's Feedback



How do you check whether a string is palindrome or not using TCL script?..

Answer / indu sharma

Hi!! i have written 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"

Is This Answer Correct ?    28 Yes 12 No

How do you check whether a string is palindrome or not using TCL script?..

Answer / nagendra prasath

proc palindrome {iStr} {
if {[ string match -nocase $iStr [string reverse $iStr]]} {
puts "Palindrome"
} else {
puts "Not a Palindrome"
}
}

palindrome "Sator Arepo Tenet Opera Rotas"
Palindrome

Is This Answer Correct ?    18 Yes 10 No

How do you check whether a string is palindrome or not using TCL script?..

Answer / ajun

set a "mumy"
set b [lindex $a 0]

set c [split $a ""]
set d [llength $c]
set e [expr $d -1]
for {set i $e} {$i >= 0} {incr i -1 } {
lappend new [lindex $c $i]
}
set f [join $new {}]
set g [string compare $a $f]
if {$g == 0} {
puts " $a is palindrome \n"
} else {
puts "$a is not palindrome\n";
}

Is This Answer Correct ?    6 Yes 1 No

How do you check whether a string is palindrome or not using TCL script?..

Answer / karthik

#without looping concept
proc pal {a} {
set b [string length $a]
set c [expr $b/2]
set d [expr $c + 1]
set e [expr $c - 1]
set strA [string range $a 0 $e]
set strB [string range $a $d $b]
set l1 [ split $strA {}]
set lA [lsort -increasing $l1]
set l2 [ split $strB {}]
set lB [lsort -increasing $l2]
set str1 [ join lA {}]
set str1 [ join $lA {}]
set str2 [ join $lB {}]
set v [string compare $str1 $str2]
if {$v == 0} {
puts "palindrome"
} else {
puts "Not a palindrome"
}
}

Is This Answer Correct ?    5 Yes 2 No

How do you check whether a string is palindrome or not using TCL script?..

Answer / s.singh

gets stdin a

set len [string length $a]
set num [expr $len/2]
set rem [expr $len%2]
set b [string range $a 0 $num-1]
if {$rem == 0} {
set c [string range $a $num $len-1]
} else {
set c [string range $a $num+1 $len-1]
}

set c [string reverse $c]
if {$b==$c} {
puts "pelindrom"
} else {
puts "not a palindrom"
}

Is This Answer Correct ?    3 Yes 2 No

How do you check whether a string is palindrome or not using TCL script?..

Answer / suraj

set str "madam" # The palindrome string
set lstr [ split $str "" ]
set len [ llength $lstr]

set i 0
set j [ expr $len - 1 ]

while {1} {
if { $j<= $i } {
puts "its palindrome"
break
} else {
set a [ lindex $lstr $i ]
set b [ lindex $lstr $j ]

if { $a != $b } {
puts "its not palindrome"
break
} else {
incr i
incr j -1
}
}
}

Is This Answer Correct ?    4 Yes 3 No

How do you check whether a string is palindrome or not using TCL script?..

Answer / indu sharma

Pseudo code:
>>Find the length of string
>>Then Compare character at index i with "i+string length"
If all character at index position i and "i+ string length" are same then it is a palindrome string.

Any better solutions???

Is This Answer Correct ?    3 Yes 3 No

How do you check whether a string is palindrome or not using TCL script?..

Answer / g

set a malayalam
set b [string length $a]
set c [split $a ""]
set b [llength $c]
set b1 [expr {$b-1}]
for {set i [expr {$b-1}]} {$i>=0} {incr i -1} {

lappend new [lindex $c $i]
}

if {$new==$c} {
puts "Palinadrome"

} else {
puts "NO paliandrome"

}

Is This Answer Correct ?    0 Yes 0 No

How do you check whether a string is palindrome or not using TCL script?..

Answer / anonymous

proc is_palindrome { input_string } {
set input_string [string tolower $input_string] ; # Convert to lower case for case insensitive comparison
set input_string [string map {"" ""} $input_string ;# Remove Spaces
set reversed_string [string reverse $input_string]
return [string equal $input_string $reversed_string]
}

# To test use is_palindrome kayak from tclsh

Is This Answer Correct ?    0 Yes 0 No

How do you check whether a string is palindrome or not using TCL script?..

Answer / 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

More Tcl Interview Questions

Which scripting language is better among TCL Perl and Python and why? 

1 Answers   GE,


how to write a startup script in winrunner

0 Answers   Power Grid, Pyxis,


WHAT IS TCL?

4 Answers   Airtel,


write a program to the given ip is valid private address or not(192.168.1.1)?

4 Answers   HCL,


How do you find the length of a string without using string length command in TCL??

6 Answers   Hexaware,






How to Swap 30 & 40 in IP address 192.30.40.1 using TCL script?

8 Answers   HCL, ONGC,


Write a program to increment IP address to +10 and verify it is correct ip or not ?? example my ip is 172.122.132.143 increment this ip to +10 and verify it is valid ip or not

1 Answers   Alcatel,


how to write the startup scripts in winrunner? can any body explain with example code?

0 Answers  


Write a proc to increment the ip by the given no. of times. The incremented IPs should be a valid one. Ex: proc <name> {ip no_of_incrments} { body }

2 Answers  


How increment a character? For example, I give 'a' and I should get 'b'.

3 Answers  


 write a regular expressions to fetch all the valid ip's

3 Answers   Alcatel,


if i give in numbers output should be in characters example : set a 23 o/p twentythree

1 Answers   Alcatel,


Categories