Kodekraftt
/Blog
/Find The Shortest Word In A Sentence In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Find the Shortest Word in a Sentence in Java

Finding the shortest word in a sentence means identifying the word with the minimum number of characters.

This problem helps improve understanding of string traversal, conditional checks, and basic logic building.

Understanding the Problem

Given a sentence, we need to:

  • Extract each word from the sentence.
  • Compare its length with the previously stored shortest word.
  • Update the shortest word when a smaller one is found.

Example 1

Input: Java programming is fun

Output: is

Example 2

Input: Find the shortest word

Output: the

Logic Explanation

  1. Traverse the sentence character by character.
  2. Store characters of the current word.
  3. When a space is encountered, compare word length.
  4. Initialize shortest word when first word is found.
  5. Check the last word after the loop ends.

Java Program to Find the Shortest Word


import java.util.Scanner;

public class ShortestWordInSentence {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a sentence: ");
        String str = sc.nextLine();

        String word = "";
        String shortestWord = "";

        for (int i = 0; i < str.length(); i++) {

            char ch = str.charAt(i);

            if (ch != ' ') {
                word = word + ch;
            } else {
                if (shortestWord.equals("") || word.length() < shortestWord.length()) {
                    shortestWord = word;
                }
                word = "";
            }
        }

        // Check last word
        if (!word.equals("") && 
            (shortestWord.equals("") || word.length() < shortestWord.length())) {
            shortestWord = word;
        }

        System.out.println("Shortest word: " + shortestWord);
    }
}

Sample Output


Enter a sentence: Java programming is fun
Shortest word: is

Important Notes

  • If multiple words have the same minimum length, the first one is returned.
  • No use of split() method.
  • Works with simple space-separated sentences.

Practice Challenges

  1. Modify the program to find all shortest words.
  2. Ignore punctuation marks while comparing words.
  3. Also print the length of the shortest word.

This program strengthens fundamental string handling and logical reasoning in Java.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.

Reverse a String in Java — Definition, Examples and Program

Learn how to reverse a string in Java using a simple and efficient approach. This blog explains the logic step by step with examples, a Java program, and practice challenges.