Write a program that finds the longest word from a sentence. Your program should read a sentence as input from the user and return the longest word. In case there are two words of maximum length return the word which comes first in the sentence.
Input and Output Format
- Input consists of a string with a maximum size of 100 characters.
- The output consists of a single string.
Refer sample output for formatting specifications
Sample Input 1:
Welcome to the world of Programming
Sample Output 1:
Programming
Sample Input 2:
ABC DEF
Sample Output 2:
ABC
Java program to find the longest word in the sentence using String Tokenizer
Following are the steps to find the longest word in the sentence:
- Input a string.
- Pass the string to the lengthiestString() method.
- Now, inside this method, first, initialize the variable max to 0 and create a new string s2.
- Use the StringTokenizer class to break the string into tokens.
- Now, iterate the string till it has tokens. Move the token into a new string and calculate the length.
- Here, check if the length is greater than the next token and store it in the s2 string, and at last return the longest token.
package testCases; import java.util.Scanner; import java.util.StringTokenizer; public class MainJava { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1= sc.nextLine(); System.out.println(lengthiestString(s1)); } public static String lengthiestString(String s1) { int max=0; String s2=new String(); StringTokenizer t=new StringTokenizer(s1," "); while(t.hasMoreTokens()){ String s3=t.nextToken(); int n=s3.length(); if(n>max){ max=n; s2=s3;} } return s2; } }
Output
Program in Java to find the Longest word in a sentence
This is another approach to finding longes word. Following are the steps to find the longest word in the sentence:
- Input a sentence.
- Now, split the sentence into words using the split() method and store it into an array of words.
- Now, set a variable longest word length to 0.
- Then, check all the words and compare the length with the longest variable length and print the longest word.
package com.date; import java.util.Scanner; public class LongestWord { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input_string = scanner.nextLine(); String [] words = input_string.split(" "); String longest_word = ""; int longest_word_len = 0; for(int i= 0; i < words.length;i++){ String word = words[i]; int len = word.length(); if(len > longest_word_len ){ longest_word = word; longest_word_len = len; } } System.out.println(longest_word); } }
Output
Longest word in a sentence using Java 8 version
We can find the longest word using Java 8 features easily. Following are the steps to achieve this:
- First, input string using nextLine() method.
- Now, use the Java 8 feature and find the longest word in one line using the below steps:
- Create an array of streams and pass the sentence to the method and split it based on the space.
- Call the method max to find the maximum which accepts the Comparator interface. Call the comapreInt() method and pass the string length using the method reference.
- And, for the other case pass null.
- At last, just print the longest word.
package com.date; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input_string = scanner.nextLine(); String longest_word = Arrays.stream(input_string.split(" ")).max(Comparator.comparingInt(String::length)).orElse(null); System.out.println(longest_word); } }
Output
Thus, in this way, we learn How to find the longest word in the string in Java.