Q:

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

belongs to collection: Java String Programs

0

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<str.length()< li=""></str.length()<>
  • 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<length< li=""></length<>
  • 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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

public class SmallestLargestWord    
   
  public static void main(String[] args){     
      String string = "Hardships often prepare ordinary people for an extraordinary destiny";    
      String word = "", small = "", large="";    
      String[] words = new String[100];    
      int length = 0;    
          
      //Add extra space after string to get the last word in the given string    
      string = string + " ";    
          
      for(int i = 0; i < string.length(); i++){    
          //Split the string into words    
          if(string.charAt(i) != ' '){    
              word = word + string.charAt(i);    
          }    
          else{    
              //Add word to array words    
              words[length] = word;    
              //Increment length    
              length++;    
              //Make word an empty string    
              word = "";    
          }    
      }            
      //Initialize small and large with first word in the string    
      small = large = words[0];    
          
      //Determine smallest and largest word in the string    
      for(int k = 0; k < length; k++){    
              
          //If length of small is greater than any word present in the string    
          //Store value of word into small    
          if(small.length() > words[k].length())    
              small = words[k];    
   
          //If length of large is less than any word present in the string    
          //Store value of word into large    
          if(large.length() < words[k].length())    
              large = words[k];    
      }    
      System.out.println("Smallest word: " + small);    
      System.out.println("Largest word: " + large);    
  }  }  

Output:

Smallest word: an
Largest word: extraordinary

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java Program to find the most repeated word in a t... >>
<< Java Program to find the frequency of characters...