Longest Word In A Sentence | Write a Program In Java to Find the Longest Word In A Sentence.| ISC Computer Science | ICSE Computer Applications

In this program, we write a program in java accept a sentence from the user and print the longest word in the sentence. 
Methods used: 
str.length(), str.substring(), str.indexOf().


Algorithm

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

Program

Buffered Reader


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

Scanner


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

Documentation

Variable Description

 Data TypeVariable NameDescription
 Stringin To store the sentence from the user 
 Stringword To store a word from the sentence 
 Stringlongest To store the longest 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 longest 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