We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 257b468 commit 967ff63Copy full SHA for 967ff63
archive/t/tcl/longest-word.tcl
@@ -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
19
+ } else {
20
+ incr curLen
21
+ }
22
23
24
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