Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class LongestWord {
- Scanner sc = new Scanner(System.in);
- String str = sc.nextLine();
- String stringArray[] = str.split("\\s");
- public String compare(String st1, String st2) {
- if (st1.length() > st2.length()) {
- return st1;
- } else {
- return st2;
- }
- }
- LongestWord() {
- String word = "";
- for (int i = 0; i < stringArray.length; i++) {
- if (i == 0) {
- word = stringArray[0];
- }
- word = compare(word, stringArray[i]);
- }
- System.out.println("Longest word is " + word + "\nWord length is " + word.length());
- }
- public static void main(String[] args) {
- new LongestWord();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment