Shortest and Longest Word Program in Java and Python
Shortest and longest word program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
Write a program to find the shortest and the longest word in a sentence and print them along with their length.
Algorithm:
Step 1: Start.
Step 2: Accept a sentence from the user.
Step 3: Add one blank space at the end of the sentence so that the last word is also processed when the loop finds a space.
Step 4: Store the length of the modified sentence in len.
Step 5: Initialize an empty string word to build each word character by character.
Step 6: Initialize longestWord and shortestWord as empty strings.
Step 7: Initialize longestLength with 0 and shortestLength with len.
Step 8: Read every character of the sentence from left to right.
Step 9: If the current character is not a blank space, append it to word.
Step 10: If the current character is a blank space and word is not empty, calculate the length of word.
Step 11: If the length is less than shortestLength, update shortestLength and shortestWord.
Step 12: If the length is greater than longestLength, update longestLength and longestWord.
Step 13: Empty word so that the next word can be formed.
Step 14: Display the shortest word, longest word and their lengths.
Step 15: Stop.
Explanation:
This program uses a character-by-character word extraction technique. Instead of directly splitting the sentence into words, it scans the sentence from the first character to the last character and builds one word at a time. Whenever the current character is not a blank space, it is joined to the temporary string word. In this way, the program gradually forms a complete word such as I, am, learning or Java.
A blank space is treated as the point where the current word ends. As soon as the loop reaches a space, the program finds the length of the word collected so far. That length is compared with two stored values: shortestLength and longestLength. If the current word is smaller than the stored shortest length, both the shortest word and its length are replaced. If the current word is larger than the stored longest length, both the longest word and its length are replaced.
The value of shortestLength is initially set to the length of the whole sentence. This is important because if it were initialized to 0, no real word length would be smaller than it. On the other hand, longestLength is initialized to 0 because any valid word will have a length greater than 0. These starting values make the first word automatically eligible to become both the shortest and the longest word until a smaller or larger word is found later.
One extra blank space is added at the end of the sentence before the loop begins. This is a common string-processing technique used in ICSE programs. Without this additional space, the last word would not be processed because the program updates the result only when it meets a blank space. After every word is checked, the temporary string is made empty so that it can store the next word. The comparison uses strict less-than and greater-than conditions, so if two words have the same length, the word that occurs first remains stored.
Java Program:
/**
* The class ShortLongWord finds the shortest and longest word in a sentence.
* It also displays the length of both words.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class ShortLongWord
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter any sentence: ");
String sentence = sc.nextLine();
/*
* A blank space is added at the end so that the last word is also
* processed when the loop reaches a space.
*/
sentence = sentence + " ";
int len = sentence.length();
String word = "";
String longestWord = "";
String shortestWord = "";
int longestLength = 0;
int shortestLength = len;
for(int i = 0; i < len; i++)
{
char ch = sentence.charAt(i);
if(ch != ' ')
{
// Build the current word until a blank space is found.
word = word + ch;
}
else if(word.length() > 0)
{
int wordLength = word.length();
// Update the shortest word only when a smaller word is found.
if(wordLength < shortestLength)
{
shortestLength = wordLength;
shortestWord = word;
}
// Update the longest word only when a longer word is found.
if(wordLength > longestLength)
{
longestLength = wordLength;
longestWord = word;
}
// Empty the temporary variable before forming the next word.
word = "";
}
}
System.out.println("Shortest word = " + shortestWord);
System.out.println("Length = " + shortestLength);
System.out.println("Longest word = " + longestWord);
System.out.println("Length = " + longestLength);
}
}Equivalent Python Program:
sentence = input("Enter any sentence: ")
# Add a blank space at the end so that the last word is also checked.
sentence = sentence + " "
word = ""
shortest_word = ""
longest_word = ""
shortest_length = len(sentence)
longest_length = 0
# Read the sentence character by character.
for ch in sentence:
if ch != " ":
# Keep forming the current word until a space is found.
word = word + ch
elif len(word) > 0:
word_length = len(word)
# Store the word if it is smaller than the current shortest word.
if word_length < shortest_length:
shortest_length = word_length
shortest_word = word
# Store the word if it is greater than the current longest word.
if word_length > longest_length:
longest_length = word_length
longest_word = word
# Clear the temporary word for the next word.
word = ""
print("Shortest word =", shortest_word)
print("Length =", shortest_length)
print("Longest word =", longest_word)
print("Length =", longest_length)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.