How do you check whether a string is palindrome or not using TCL script?
Answers were Sorted based on User's Feedback
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 ? | 29 Yes | 12 No |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Problems with utf-8 between Mac and PC
How to Swap 30 & 40 in IP address 192.30.40.1 using TCL script?
Which scripting language is better among TCL Perl and Python and why?
Hi all, Is there any certification exams available for TCL and Perl. If so please let me know, my mailid is vpbharathi@gmail.com. Thanks in advance, Bharathi.P
write a program to the given ip is valid private address or not(192.168.1.1)?
Test case on windows calculator?
how to write the startup scripts in winrunner? can any body explain with example code?
How do you check whether a string is palindrome or not using TCL script?
how to write a startup script in winrunner
How to extract "information" from "ccccccccaaabbbbaaaabbinformationabcaaaaaabbbbbbbccbb" in tcl using a single command?
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
{Anu Anudeep Anukumar Amar Amaravathi Aruna} is their any possibility to find the letter "a"in the given list? if yes how?