Longest Word

Published on 13 May 2022 (Updated: 22 January 2024)

Welcome to the Longest Word page! Here, you'll find a description of the project as well as a list of sample programs written in various languages.

This article was written by:


Given a string, this program should break it up into words and determine the length of the longest word. In this case, a word is defined as anything surrounded by whitespace. For simplicity, we'll restrict whitespace to the following four special characters:

For example, if we had a string, "How now brown cow", we can figure out which word is the longest by breaking up the string into words. In this case, this string has the following four words:

In this case, "brown" is clearly the longest word, so we'll return 5 as a result.


To satisfy the requirements, a program must accept a string on the command line and return the length of the longest word in the string:

$ ./longest-word-in-string.lang "Google do a barrel roll"
$ 6

In this case, we have a string with 5 words. It appears that there are two words that share the largest number of characters: Google and barrel. Naturally, we don't care to decide between the two words. Instead, we return the length of them both: 6.


Every project in the Sample Programs repo should be tested. In this section, we specify the set of tests specific to Longest Word. In order to keep things simple, we split up the testing as follows:

Longest Word Valid Tests

Description Input Output
Sample Input: Many Words "May the force be with you" "5"
Sample Input: Single Word "Floccinaucinihilipilification" "29"
Sample Input: Multiline "Hi,\nMy name is Paul!" "5"

Longest Word Invalid Tests

Description Input
No Input  
Empty Input ""

All of these tests should output the following:

Usage: please provide a string