Monday, 27 February 2017

TCL INTERVIEW QUESTIONS -Part2

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

set var “192.30.40.1″
set list1 [split $var "."]
set list2 [lreplace $list1 1 2 40 30]
set result [join $list2 "."]
puts $result
o/p:
192.40.30.1

Using regsub it can be swapped in two lines or one line also as below  

ALITER
set ip 192.30.40.1
regsub {(\d\d).(\d\d).(\d\d).(\d\d)} $ip {\1.\3.\2.\4} ip1
puts $ip1

ALITER
set ip 192.30.40.1
regsub {(\d+).(\d+).(\d+).(\d+)} $ip {\1.\3.\2.\4} ip1
puts $ip1

ALITER

set a 192.30.40.1
set b [ split $a .]
set u [lindex $b 0]
set v [lindex $b 3]
set x [lindex $b 1]
set y [lindex $b 2]
set z [join "$u $y $x $v" .]
puts $z

ALITER
set ip 192.30.40.1
regexp {([0-9]+\.)([0-9]+\.)([0-9]+\.)([0-9]+)} $ip match 1st 2nd 3rd 4th
append new_ip $1st $3rd $2nd $4th
puts $new_ip


2. Set ip address as 10.30.20.1 write a script to replace the 30 with 40.

set var “10.30.20.1″
regsub 30 $var 40 result
puts $result
o/p:
10.40.20.1

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

proc palindrome {str} {
set l [string length $str]
set i 0
incr l -1
set flag 0
while {$l>=0} {
set s [string index $str $i]
set e [string index $str $l]
if {$s==$e} {  } else {
set flag 1
break }
incr l -1
incr i 1
}
if {$flag ==0} { puts “The given string $str is palindrome” } else {
puts “The given string $str is not palindrome” }
}
palindrome “malayalam”
palindrome “welcome”
o/p:
The given string malayalam is palindrome
The given string welcome is not palindrome

ALITER
set a madam
set len [string length $a]
set n [expr ($len-1)/2]
for {set i 0} {$i < $n} {incr i} {
set b [string index $a $i]
set c [expr $len - 1 - $i]
set d [string index $a $c]
if {$b != $d} {
puts "not palindrome"
exit
}
}
puts "palindrome"

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

set ch a
set l [scan $ch %c]
incr l
set m [format %c $l]
puts "$m"


5.{Anu Anudeep Anukumar Amar Amaravathi Aruna}
is their any possibility to find the letter "a"in the given list? if yes how?

puts "searching variable a"
set list_4 [list Anu Anudeep Anukumar Amar Amaravathi Aruna]
foreach item $list_4 {
set elem [split $item ""]
foreach var $elem {
if {$var == "a"} {
puts $item
}
}
}

ALITER

set str {Anu Anudeep Anukumar Amar Amaravathi Aruna}
foreach i $str {
if {[regexp {(A)} $i match ] } {
puts $match
}
}

6.Create a list of week days and print the first and last
character of each day using foreach command.
 % set days {monday tuesday wednasday thursday friday
saturday sunday}
monday tuesday wednasday thursday friday saturday sunday
% foreach day $days {
set str [split $day ""]
puts "[lindex $str 0]"
puts "[lindex $str end]"
}

7. 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
}
proc increment_ip {ip no_of_inc} {
puts "Ip address --- $ip"
for {set inc 1} {$inc<=$no_of_inc} {incr inc} {
set ip_list [split $ip "."]
set oct1 [lindex $ip_list 0]
set oct2 [lindex $ip_list 1]
set oct3 [lindex $ip_list 2]
set oct4 [lindex $ip_list 3]
incr oct4
if {$oct4>255} {
set oct4 0
incr oct3
}
if {$oct3>255} {
set oct3 0
incr oct2
}
if {$oct2>255} {
set oct2 0
incr oct1
}
if {$oct1>255} {
incr oct1 -1
puts "cannot increment ip"
exit
}
set ip $oct1.$oct2.$oct3.$oct4
puts "next ip -- $ip"
}
}

increment_ip 1.1.1.1 10


8. How to get the next ip for given ip
ex: 10.10.10.1 -> 10.10.10.2
ex: 10.10.10.255 -> 10.10.11.0

set ip "10.10.10.255"
set list [split $ip "."]
set ww [lindex $list 0]
set xx [lindex $list 1]
set yy [lindex $list 2]
set zz [lindex $list 3]

if {$zz>254} {
set zz 0
incr yy
if {$yy>255} {
set yy 0
incr xx
if {$xx>255} {
set xx 0
incr ww }
if {$ww>255} {
set ww 0
}}} else {
incr zz
}
puts "$ww.$xx.$yy.$zz"

No comments:

Post a Comment

Note: only a member of this blog may post a comment.