Skip to content

Commit 967ff63

Browse files
authored
Add Longest Word in Tcl (#5067)
1 parent 257b468 commit 967ff63

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

archive/t/tcl/longest-word.tcl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
proc usage {} {
2+
puts stderr {Usage: please provide a string}
3+
exit 1
4+
}
5+
6+
proc longestWordLength {s} {
7+
set s [string trim $s]
8+
if {$s eq ""} { usage }
9+
10+
set maxLen 0
11+
set curLen 0
12+
set len [string length $s]
13+
14+
for {set i 0} {$i < $len} {incr i} {
15+
set ch [string index $s $i]
16+
if {[string is space $ch]} {
17+
if {$curLen > $maxLen} { set maxLen $curLen }
18+
set curLen 0
19+
} else {
20+
incr curLen
21+
}
22+
}
23+
24+
if {$curLen > $maxLen} { set maxLen $curLen }
25+
26+
return $maxLen
27+
}
28+
29+
if {$argc != 1} { usage }
30+
31+
set input [lindex $argv 0]
32+
if {[string trim $input] eq ""} { usage }
33+
34+
puts [longestWordLength $input]

0 commit comments

Comments
 (0)