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 ? | 30 Yes | 13 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 ? | 19 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 ? | 7 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 |
if i give in numbers output should be in characters example : set a 23 o/p twentythree
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
How to extract "information" from "ccccccccaaabbbbaaaabbinformationabcaaaaaabbbbbbbccbb" in tcl using a single command?
Which scripting language is better among TCL Perl and Python and why?
how to remote log in to a system A to system B ,execute commands in it and collect the log in system A from B using TCL script??
1 Answers Global Edge, Sandvine,
how to write the startup scripts in winrunner? can any body explain with example code?
Where can find the sample tcl programs?
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 }
Set ip address as 10.30.20.1 write a script to replace the 30 with 40 ?
How do you find the length of a string without using string length command in TCL??
How increment a character? For example, I give 'a' and I should get 'b'.
write a regular expressions to fetch all the valid ip's