TCL script examples with sample programs
1.TCL Program – count letters in the given string using tcl
set str “LIHAKHDBLICIHJAADFDCSDBBBDFDB”
set l [string length $str]
puts $l
set cnt_A 0
set cnt_B 0
set cnt_C 0
set i 0
while {$i<=$l} {
if {“A”==[string index $str $i]} {
incr cnt_A } elseif {“B”==[string index $str $i]} {
incr cnt_B } elseif {“C”==[string index $str $i]} {
incr cnt_C }
incr i
}
set l [string length $str]
puts $l
set cnt_A 0
set cnt_B 0
set cnt_C 0
set i 0
while {$i<=$l} {
if {“A”==[string index $str $i]} {
incr cnt_A } elseif {“B”==[string index $str $i]} {
incr cnt_B } elseif {“C”==[string index $str $i]} {
incr cnt_C }
incr i
}
puts “The count of A is $cnt_A \n
The count of B is $cnt_B \n
The count of C is $cnt_C”
The count of B is $cnt_B \n
The count of C is $cnt_C”
o/p:
29
The count of A is 3
29
The count of A is 3
The count of B is 5
The count of C is 2
2.TCL Program- Factorial value with and without using recursion in tcl
proc fact { n } {
set f 1
while {$n>=2} {
set f [expr {$f*$n}]
incr n -1
}
return $f
}
set f 1
while {$n>=2} {
set f [expr {$f*$n}]
incr n -1
}
return $f
}
proc recfact n {
if {$n<=1} {
return 1 }
expr $n * [recfact [expr {$n-1}]]
}
if {$n<=1} {
return 1 }
expr $n * [recfact [expr {$n-1}]]
}
puts “The factorial value without recursion [fact 4]”
puts “The factorial value with recursion [recfact 4]”
puts “The factorial value with recursion [recfact 4]”
3.TCL Program – Find maximum number in the given 3 numbers using tcl
set a 10
set b 20
set c 15
if {$a>$b && $a>$c} {
puts “a is bigger and value is $a” } elseif {$b>$a && $b>$c} {
puts “b is bigger and value is $b” } else {
puts “c is bigger and value is $c” }
set b 20
set c 15
if {$a>$b && $a>$c} {
puts “a is bigger and value is $a” } elseif {$b>$a && $b>$c} {
puts “b is bigger and value is $b” } else {
puts “c is bigger and value is $c” }
o/p:
b is bigger and value is 20
b is bigger and value is 20
4.TCL Program – reverse string using tcl
proc strrev {str} {
set l [string length $str]
set l [expr $l-1]
set rev {}
for {set i $l} {$i>=0} {incr i -1} {
append rev [string index $str $i] }
puts “$rev”
}
set l [string length $str]
set l [expr $l-1]
set rev {}
for {set i $l} {$i>=0} {incr i -1} {
append rev [string index $str $i] }
puts “$rev”
}
strrev “welcome”
o/p:
emoclew
emoclew
5.TCL Program – Check given number is odd or even in tcl
proc oddeven {n} {
if {$n%2==0} {
puts “the given $n is even” } else {
puts “the given number $n is odd” }
if {$n%2==0} {
puts “the given $n is even” } else {
puts “the given number $n is odd” }
}
oddeven 24
oddeven 67
o/p:
the given 24 is even
the given number 67 is odd
the given 24 is even
the given number 67 is odd
This comment has been removed by the author.
ReplyDeleteGood basic programs but very simple. Please add Fibonacci, Polyndrome, Armstrong,Grepping IP pattern, right angle-triangle.. These will be frequently asked in interview
ReplyDeleteThanks Vidhun,you have mentioned good programs which I have missed.I will add the same.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete