Java Program to find the largest and smallest word in a string.

In this program, we need to find the smallest and the largest word present in the string:

Hardships often prepare ordinary people for an extraordinary destiny

Consider above example in which ‘an’ is the smallest word and ‘extraordinary’ is the largest word. One of the approach to find smallest and largest word is to split string into words then, compare length of each word with variables small and large. If length of a word is less than length of small then, store that word in small. If length of a word is greater than length of large then, store that word in large.

ALGORITHM

  • STEP 1: START
  • STEP 2: DEFINE String string=”Hardships often prepare ordinary people for an extraordinary destiny”
  • STEP 3: DEFINE word = ” “, small = ” “, large = ” “.
  • STEP 4: Make object of String[] words.
  • STEP 5: SET length =0
  • STEP 6: string = string + ” “
  • STEP 7: SET i=0. REPEAT STEP 8 to 9 STEP UNTIL i
  • STEP 8: IF(string.charAt(i) != ‘ ‘) then
                  word =word + string.charAt(i)
                  else
                  word[length]=word
                  length =length + 1
                  word = ” “
  • STEP 9: i=i+1
  • STEP 10: small = large =words[0]
  • STEP 11: SET k = 0. REPEAT STEP 12 to STEP 14 UNTIL k
  • STEP 12: IF(small.length() > words[k].length())
                  then
                  small = words[k]
  • STEP 13: IF(large.length() < words[k].length())
                  then
                  large = words[k]
  • STEP 14: k = k + 1
  • STEP 15: PRINT small
  • STEP 16: PRINT large
  • STEP 17: END

Program:

  1. public class SmallestLargestWord    
  2.    
  3.   public static void main(String[] args){     
  4.       String string = “Hardships often prepare ordinary people for an extraordinary destiny”;    
  5.       String word = “”, small = “”, large=””;    
  6.       String[] words = new String[100];    
  7.       int length = 0;    
  8.           
  9.       //Add extra space after string to get the last word in the given string    
  10.       string = string + ” “;    
  11.           
  12.       for(int i = 0; i < string.length(); i++){    
  13.           //Split the string into words    
  14.           if(string.charAt(i) != ‘ ‘){    
  15.               word = word + string.charAt(i);    
  16.           }    
  17.           else{    
  18.               //Add word to array words    
  19.               words[length] = word;    
  20.               //Increment length    
  21.               length++;    
  22.               //Make word an empty string    
  23.               word = “”;    
  24.           }    
  25.       }            
  26.       //Initialize small and large with first word in the string    
  27.       small = large = words[0];    
  28.           
  29.       //Determine smallest and largest word in the string    
  30.       for(int k = 0; k < length; k++){    
  31.               
  32.           //If length of small is greater than any word present in the string    
  33.           //Store value of word into small    
  34.           if(small.length() > words[k].length())    
  35.               small = words[k];    
  36.    
  37.           //If length of large is less than any word present in the string    
  38.           //Store value of word into large    
  39.           if(large.length() < words[k].length())    
  40.               large = words[k];    
  41.       }    
  42.       System.out.println(“Smallest word: ” + small);    
  43.       System.out.println(“Largest word: ” + large);    
  44.   }  }  

Output:

Smallest word: an
Largest word: extraordinary

Leave a Comment