Computer Science, asked by puja9906595, 11 months ago

write a program in Java to enter a string/ sentence and display the longest word and the length of the longest word present in the string. Sample intput: "Tata Football Academy will play against Mohan bagan". Sample output: the longest word : football. The length of the word :8. By using util. *package. ​

Answers

Answered by QGP
36

Longest Word Program - Java

We will be using the java.util.Scanner for the purpose. We create a Scanner object to take a String input.

After that, we create a new Scanner object and pass the String to it. So, we can now iterate over the String.

The Scanner object considers the String as a sequence of tokens. The tokens are marked separate by a space by default. So, different words are the different tokens for the Scanner object.

The hasNext() function of Scanner returns true if there is a token present next from the current position. Here, it will return true if a word is present next.

Then we can use the length() function of Strings to get the length of token (here, word). We can then run a while loop until hasNext() returns true and find the length of all the words.

Over this loop, we can keep comparing the longest word length to the current word length. If the current word length is more, then the longest word and the maximum length variables are updated.

The Program is given below. Note that the program would consider any punctuation marks as a part of the word. So, input a String without any punctuation marks, as shown in the Sample Input.

A Screenshot shows the Output of the program run three times.

 \rule{300}{1}

import java.util.*;      

public class LongestWord

{

   public static void main(String[] args)

   {

       Scanner sc = new Scanner(System.in);

       //Take user input for string

       System.out.print("Enter a String or Sentence: ");

       String str = sc.nextLine(); //Read complete line and store in str

       //Create new Scanner object scstr with str as its input  

       Scanner scstr = new Scanner(str);  

       //Initialise longestWord with the first word in str

       String longestWord = scstr.next();

       //Initiaise maxlen with length of first word in str

       int maxlen = longestWord.length();

       

       while(scstr.hasNext())  //This loop will keep running till words are present

       {

           String word = scstr.next(); //Storing next word in variable

           int len = word.length();    //Storing word's length

           if(len>maxlen)  //If this length is more than maxlen, longestWord and maxlen are changed

           {

               longestWord = word;  

               maxlen = len;

           }

       }

       System.out.println("The longest word is '"+longestWord+"' of length "+maxlen+" characters.");

   }

}

Attachments:
Answered by SombuddhaBiswas
8

import java.util.*;

public class LongestWord

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

//Take user input for string

System.out.print("Enter a String or Sentence: ");

String str = sc.nextLine(); //Read complete line and store in str

//Create new Scanner object scstr with str as its input

Scanner scstr = new Scanner(str);

//Initialise longestWord with the first word in str

String longestWord = scstr.next();

//Initiaise maxlen with length of first word in str

int maxlen = longestWord.length();

while(scstr.hasNext()) //This loop will keep running till words are present

{

String word = scstr.next(); //Storing next word in variable

int len = word.length(); //Storing word's length

if(len>maxlen) //If this length is more than maxlen, longestWord and maxlen are changed

{

longestWord = word;

maxlen = len;

}

}

System.out.println("The longest word is '"+longestWord+"' of length "+maxlen+" characters.");

}

}

Similar questions