Program to Search Largest & Smallest Word in a String

Program to Search Largest & Smallest Word in a String

In this Program, we will be taking the input sentence and after taking the input sentence we will search for the largest and smallest word . There are various ways to write code for this topic, but I have found the easiest one for you. Just go through the algorithm you will understand the logic behind this code.

Note: For Mobile User, Please view it in Desktop mode

Algorithm/Program Explanation

  1. Start
  2. static public void Search(String string)// defining search method
  3. Define and Initialize the following variables
    • String str= string+””;//for extracting last word
    • char ch= ‘ ‘;
    • int length=str.length();// Storing length of str
    • int count=0,minimum=length,maximum=0;
    • String smallest=””, largest=””,word=””;
  4. Define and Initialize For loop
    • for(int i=0;i<length;i++)
    • ch=str.charAt(i)
    • Check for Condition
      • if(ch!=’ ‘) if character which is not equal to space then form words
      • word+=ch;//here we are forming words from character excluding spaces
      • else
      • count=word.length();
      • Check for Condition
        • if(count< minimum)//checking for minimum length
        • minimum=count
        • smallest=word;
        • if(count> maximum)//Checking for maximum length
        • maximum=count
        • largest=word;
      • word=””// emptying temporary variable to store new words
  5. print smallest word
  6. print largest word
  7. end

Program to Search Largest & Smallest Word in a String:

/* 
Java Program To search largesr and smallest word in the sgiven string */
import java.util.*;
public class LargestSmallestword{


    public static void main(String args[]){
        Search("My Name is NotesHacker and it is best Website for Learning");
    }
    static public void Search(String string)// defining search method 
     {
        String str= string+"";//for extracting last word
        char ch= ' ';
        int length=str.length();
        int count=0;int minimum=length,maximum=0;
        String smallest="", largest="",word="";
        for(int i=0;i<length;i++){
            ch=str.charAt(i);
            if(ch!=' '){
                word+=ch;//here we are forming words from character excluding spaces
            }
            else{
                count=word.length();
                if(count< minimum)//checking for minimum length
               {
                    minimum=count;
                    smallest=word;
                }
                if(count> maximum){
                    maximum=count;
                   largest=word;
                }
                word="";// emptying variable for storing next word
            }
        }
        System.out.println("Smallest Word=" + smallest + " length=" + minimum);
        System.out.println("Largest Word=" + largest + " length=" + maximum);
    }
}
Output:
Smallest Word=My length=2
Largest Word=NotesHacker length=11

Related Topics

Leave a Comment