Find longest word in a given string but the word only have even length | Sololearn: Learn to code for FREE!
Nowy kurs! Każdy programista powinien nauczyć się Generative AI!
Wypróbuj darmową lekcję
- 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

6th Nov 2017, 5:21 AM
Mohan
Mohan - avatar
4 odpowiedzi
+ 5
Mohan why are your questions always urgent
6th Nov 2017, 8:54 AM
David Akhihiero
David Akhihiero - avatar
+ 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 "); } }
5th Oct 2018, 8:19 PM
amit KAMATH
amit KAMATH - avatar
+ 1
it's exercise to do and I am very thankful ... thank you Ace
6th Nov 2017, 9:40 AM
Mohan
Mohan - avatar
+ 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.
17th Jun 2019, 8:11 PM
Vatsal Shah
Vatsal Shah - avatar