Java ProgramsStringsFind Shortest Word in a String

Find Shortest Word in a String in Java

beginner·  Strings  ·  String

Problem

The shortest word in a sentence is whichever word has the fewest characters once every word's length has been compared.

Given a sentence, find its shortest word.

Input
The quick brown fox jumps
Output
Shortest word: The

Java Program

Java
public class FindShortestWord { public static void main(String[] args) { String str = "The quick brown fox jumps"; String[] words = str.split(" "); String shortest = words[0]; for (String word : words) { if (word.length() < shortest.length()) { // update whenever a strictly shorter word is found shortest = word; } } System.out.println("Shortest word: " + shortest); } }

Output

Shortest word: The

Core Logic

Splitting the sentence into words and keeping track of the shortest one seen so far, one pass through the array, is the mirror image of finding the longest word.

How It Works
  1. 1str.split(" ") breaks the sentence into an array of individual words.
  2. 2shortest starts out holding the first word, words[0].
  3. 3A loop visits every word, comparing its length against shortest.length() with &lt;.
  4. 4Whenever a shorter word is found, shortest is updated to that word.
For "The quick brown fox jumps", 'The' and 'fox' both tie at 3 letters — shortest is set to 'The' first and, since 'fox' isn't strictly shorter, it stays 'The'.
💡

Key Point: Using &lt; rather than &lt;= means the first word to reach the minimum length wins any tie, keeping the result deterministic.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: split() allocates an array holding every word, and the scan then makes one pass over it comparing lengths.

Key Concepts

String.split()String.length()running minimum

Approach 2: Java 8

Java
import java.util.Arrays; import java.util.Comparator; public class FindShortestWordStream { public static void main(String[] args) { String str = "The quick brown fox jumps"; // Compares every word by length and keeps the shortest one String shortest = Arrays.stream(str.split(" ")) .min(Comparator.comparingInt(String::length)) .orElseThrow(); System.out.println("Shortest word: " + shortest); } }

Output

Shortest word: The

Core Logic

Once the words are split, a stream can find the shortest one directly instead of a manual running-minimum loop.

How It Works
  1. 1Arrays.stream(str.split(" ")) turns the split words into a Stream<String>.
  2. 2Comparator.comparingInt(String::length) builds a comparator that compares two words purely by their length.
  3. 3.min(...) compares every word using that comparator and keeps the single shortest one, wrapped in an Optional.
  4. 4.orElseThrow() unwraps the result, since the sentence is known not to be empty here.
Comparing every word in "The quick brown fox jumps" by length picks out 'The' as the shortest, matching the manual version's first-seen tiebreak.
💡

Key Point: The only difference from finding the longest word is min() instead of max() — the same comparator works for both.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: split() still allocates an array holding every word, and min() then makes one pass over it comparing lengths.

Key Concepts

StreamComparator.comparingInt()min()

Related Programs