Skip to content

Instantly share code, notes, and snippets.

@raviyasas
Created April 25, 2020 20:12
Show Gist options
  • Save raviyasas/83925b4ab7899f12908bf87c5e8be19e to your computer and use it in GitHub Desktop.
Save raviyasas/83925b4ab7899f12908bf87c5e8be19e to your computer and use it in GitHub Desktop.
Longest word check
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