Longest Word in Tcl

Published on 09 October 2025 (Updated: 09 October 2025)

Welcome to the Longest Word in Tcl page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

proc usage {} {
	puts stderr {Usage: please provide a string}
	exit 1
}

proc longestWordLength {s} {
	set s [string trim $s]
	if {$s eq ""} { usage }

	set maxLen 0
	set curLen 0
	set len [string length $s]

	for {set i 0} {$i < $len} {incr i} {
		set ch [string index $s $i]
		if {[string is space $ch]} {
			if {$curLen > $maxLen} { set maxLen $curLen }
			set curLen 0
		} else {
			incr curLen
		}
	}

	if {$curLen > $maxLen} { set maxLen $curLen }

	return $maxLen
}

if {$argc != 1} { usage }

set input [lindex $argv 0]
if {[string trim $input] eq ""} { usage }

puts [longestWordLength $input]

Longest Word in Tcl was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.