Find the Length of the Longest Word in a String

Given a string of words, this function will return the length of the longest word. For example, given the input string "The quick brown fox jumps over the lazy dog", the function will return 6, since "quick" is the longest word in the string.

Problem

Given a string of words, return the length of the longest word.
Example input: "The quick brown fox jumps over the lazy dog"
Example output: 6 (for the word "quick")

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def longest_word(string):
    words = string.split()
    longest = 0
    for word in words:
        if len(word) > longest:
            longest = len(word)
    return longest

print(longest_word("The quick brown fox jumps over the lazy dog"))

A.I. Evaluation of the Solution

This solution correctly finds the length of the longest word in a string. It splits the string into a list of words, then loops through the list to find the longest word. This is a good approach.

Evaluated at: 2022-11-18 06:16:07