Longest Word in Lua

Published on 26 March 2025 (Updated: 26 March 2025)

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

Current Solution

if #arg < 1 then
    print("Usage: please provide a string")
    os.exit(1)
end

local input = arg[1]

function longest_word_length(str)
    local max_length = 0
    local found_word = false

    for word in string.gmatch(str, "%S+") do
        found_word = true
        local length = #word  
        if length > max_length then
            max_length = length  
        end
    end

    if not found_word then
        print("Usage: please provide a string")
        os.exit(1)
    end

    return max_length  
end


print(longest_word_length(input))

Longest Word in Lua 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.