- 2
Find longest word in a given string but the word only have even length
if input is "today is a good day" then output is "good" because it has even length and longest even word
4 Answers
+ 5
Mohan why are your questions always urgent
+ 2
public class LongestEven {
public static void Long(String Str) {
String[] strarray = Str.split(" ");
String Evenword="";
for(String st : strarray) {
if(st.length()%2==0)
{
if(Evenword.length()<st.length()) {
Evenword=st;
}
}
}System.out.print("Word is an Even and is " +Evenword);
}
public static void main(String args[]) {
Long(" abababababab I AM SHSHSHSHS KAM ");
}
}
+ 1
it's exercise to do and I am very thankful ... thank you Ace
+ 1
I was asked this question in Bloomberg Software Engineer Interview. The one modification I can suggest is rather than splitting with space, traverse character by character until you encounter space. Form words from the interval of previous and current space. This way you will not store a big array in storage. This concern was pointed out by my interviewer as I split the string and he told me that consider a case where the input string is too large or real-time input stream then how this will impact and hence I corrected my solution with traversal character by character. Hope this helps.