Finding the longest word in a sentence

Report a typo

Write a Java program that receives a string representing a sentence. Your program should find the longest word in the sentence and print it out. Assume that the words in the sentence are separated by a single space and there are no punctuation marks.

Sample Input 1:

This is a sample sentence

Sample Output 1:

sentence

Sample Input 2:

Java is a powerful language

Sample Output 2:

powerful
Write a program in Java 17
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String sentence = scanner.nextLine();
// Your code here
}
}

Create a free account to access the full topic