Created
April 25, 2020 20:12
-
-
Save raviyasas/83925b4ab7899f12908bf87c5e8be19e to your computer and use it in GitHub Desktop.
Longest word check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.interviews; | |
public class LongestWord { | |
public static void main(String[] args) { | |
String s = "Hi, May I know your name please? "; | |
String[] array = s.split(" "); | |
String rts = ""; | |
for (int i = 0; i < array.length; i++) { | |
if (array[i].length() > rts.length()) { | |
rts = array[i]; | |
} | |
} | |
System.out.println(rts); | |
// Using foreach | |
for (String word : array) { | |
if (word.length() > rts.length()) { | |
rts = word; | |
} | |
} | |
System.out.println(rts); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment