Tuesday, June 8, 2021

TCL Programming Basic Practice Exercises /Examples 2

🧠 TCL Programming Basic Practice Exercises / Examples (Part 2)

Hello friends! 👋
Welcome once again!

In this post, we’ll explore a few basic TCL programming exercises that will help you build logic, strengthen your scripting skills, and gain confidence in automation coding.


🔹 1. Find the Number of Vowels in a String

set a "Rushikesh Patil" set b 0 set c [string length $a] for {set i 0} {$i < $c} {incr i} { set d [string index $a $i] if {$d == "a" || $d == "e" || $d == "i" || $d == "o" || $d == "u"} { incr b } } puts "The number of vowels are = $b"

🔹 2. Random Number Output

for {set i 0} {$i < 100} {incr i} { set random_number [expr int(rand() * 10)] puts $random_number }

🔹 3. Factorial Program (With and Without Recursion)

➤ Without Recursion

proc fact {n} { set f 1 while {$n >= 2} { set f [expr {$f * $n}] incr n -1 } return $f }

➤ With Recursion

proc recfact {n} { if {$n <= 1} { return 1 } expr {$n * [recfact [expr {$n - 1}]]} } puts "Factorial without recursion = [fact 4]" puts "Factorial with recursion = [recfact 4]"

🔹 4. Fibonacci Series

set fib0 0 set fib1 0 set fib2 1 set s "" for {set i 0} {$i < 10} {incr i} { set fib3 [expr {$fib1 + $fib2}] set fib1 $fib2 set fib2 $fib3 append s "$fib1, " } puts "$fib0, $s" proc fib {n} { return [expr {$n < 2 ? $n : [fib [expr {$n - 1]]] + [fib [expr {$n - 2]]}}] }

🔹 5. Count Letters in a String

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 } puts "The count of A is $cnt_A" puts "The count of B is $cnt_B" puts "The count of C is $cnt_C"

🌟 Summary

These simple TCL programs help you understand the fundamentals of loops, conditions, recursion, and string operations.
Practicing such examples builds your confidence and prepares you for automation scripting in CAE and engineering workflows.

Thank you for reading! 🙏
Keep practicing and stay tuned for more exciting TCL examples in upcoming posts.

No comments:

Post a Comment

Thank you !

Time

Search This Blog

Contact Form

Name

Email *

Message *