Shortest Word In A Sentence | Write a program in Java to find the shortest word in a sentence. | ISC Computer Science | ICSE Computer Application

Image is Unavailable


Algorithm

  1. Start
  2. in=""
  3. word=""
  4. shortest=""
  5. Print "Enter a sentence"
  6. Read in
  7. in=in+" "
  8. shortest=in
  9. If in.equals("")==false, go to step 10 or else go to step 15
  10. word=in.substring(0, in.indexOf(' '))
  11. If word.length()<shortest.length(), go to step 12 or else go to step 13
  12. shortest=word
  13. in=in.substring(in.indexOf(' ')+1, in.length())
  14. Go to step 9
  15. Print "The shortest word is "+shortest
  16. Stop

Program

Buffered Reader

import java.io.*;
class Shortest
{
    String in, word="", shortest="";
    
    void accept() throws IOException
    {
        BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
        System.out.println("Enter a sentence");
        in=br.readLine();
        in=in+" ";
        shortest=in;
    }
    
    void shortest()
    {
        while(in.equals("")==false)
        {
            word=in.substring(0, in.indexOf(' '));
            if(word.length()<shortest.length())
            {
                shortest=word;
            }
            in=in.substring(in.indexOf(' ')+1, in.length());
        }
    }
    
    void display()
    {
        System.out.println("The shortest word is "+shortest);
    }
    
    void main() throws IOException
    {
        accept();
        shortest();
        display();
        }
}

Scanner

import java.util.*;
class Shortest
{
    String in, word="", shortest="";
    
    void accept() 
    {
        Scanner sc=new Scanner (System.in);
        System.out.println("Enter a sentence");
        in=sc.nextLine();
        in=in+" ";
        shortest=in;
    }
    
    void shortest()
    {
        while(in.equals("")==false)
        {
            word=in.substring(0, in.indexOf(' '));
            if(word.length()<shortest.length())
            {
                shortest=word;
            }
            in=in.substring(in.indexOf(' ')+1, in.length());
        }
    }
    
    void display()
    {
        System.out.println("The shortest word is "+shortest);
    }
    
    void main() 
    {
        accept();
        shortest();
        display();
        }
}

Documentation

Variable Description

Data TypeVariable NameDescription
 Stringin To store the sentence from the user 
 Stringword To store a word from the sentence 
 StringshortestTo store the shortest word of the sentence 

Method Description

 Return TypeMethod NameDescription
 voidaccept() To accept a sentence from the user 
 voidlongest() To find the longest word in a sentence 
 void display() To display the shortest word 
 voidmain() To act as an entry gateway for the compiler. 

Output



Image is Unavailable



Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Reddit: @ISCJavaCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode


Stay Healthy
Stay Happy
Stay Helpful

Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Conversion of Compound Sentences to Complex Sentences.

Conversion of Complex Sentences to Compound Sentences