Java Program to Find the Largest and Smallest Word in a String

This is the Java Program to Find the Largest and Smallest Word

Problem Description

Given a string of characters, find the largest and the smallest word if two or more words are the largest or smallest then display the one that occurs first in the string.

Example:

String x = “We belong to Russia”
Output = We, belong

“We” and “to” both are the smallest words since “We” is encountered first, it comes in the output, same is with “belong” and “Russia”.

Problem Solution

Split the given string into various substrings, whenever space is encountered. Iterate through these substrings to find the first largest and smallest word.

advertisement
advertisement
Program/Source Code

Here is the source code of the Java Program to Find the Largest and Smallest Word. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. // Java program to Find the Largest and Smallest Word.
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class LargestAndSmallestWord {
  8.     // Method to split the string and find the largest and smallest word 
  9.     static void printLargestAndSmallestWord(String str){
  10.         String[] arr=str.split(" ");
  11.         int i=0;
  12.         int maxlength,minlength;
  13.         maxlength=Integer.MIN_VALUE;
  14.         minlength=Integer.MAX_VALUE;
  15.         String largest,smallest;
  16.         largest = smallest = "";
  17.         for(i=0;i<arr.length;i++){
  18.             if(arr[i].length() < minlength){
  19.                 smallest=arr[i];
  20.                 minlength=arr[i].length();
  21.             } 
  22.             if(arr[i].length() > maxlength) {
  23.                 largest=arr[i];
  24.                 maxlength=arr[i].length();
  25.             }
  26.         }
  27.         System.out.println("The largest and smallest word is \"" + largest +
  28.                                                "\" and \"" + smallest + "\"");
  29.     }
  30.     // Main function to read the string
  31.     public static void main(String[] args) {
  32.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  33.         System.out.println("Enter the text string");
  34.         String str;
  35.         try{
  36.             str=br.readLine();
  37.         }
  38.         catch(Exception e){
  39.             System.out.println("Error reading input");
  40.             return;
  41.         }
  42.         printLargestAndSmallestWord(str);
  43.     }
  44. }
Program Explanation

1. In function printLargestAndSmallestWord(), the string is split into various tokens using s.split(” “) statement.
2. The minlength and maxlength variables are initialized to Integer.MAX_VALUE and Integer.MIN_VALUE, respectively.
3. The loop for(i=0;i<arr.length;i++) is used to iterate through the array of tokens.
4. The condition if(arr[i].length() < minlength) checks if the current token is the smallest, if it is the minlength and smallest variables are updated accordingly.
5. The condition if(arr[i].length() > maxlength) checks if the current token is the largest, if it is the maxlength and largest variables are updated accordingly.

Time Complexity: O(n) where n is the length of the string.

Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the text string
I am IronMan
The largest and smallest word is "IronMan" and "I"
 
Case 2 (Simple Test Case - another example):
 
Enter the text string
We belong to Russia
The largest and smallest word is "belong" and "We"

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.