How to find largest word in a given string using Java with O(n)


package com.onlinecodegeek.java;

public class FindLargetWord {

public static void main(String args[]) {
String s = " Welcome to Java World";
String[] wordsArray = s.split(" ");
int maxsize = 0;
String maxWord = "";
for (int i = 0; i < wordsArray.length; i++) {
if (wordsArray[i].length() > maxsize) {
maxWord = wordsArray[i];
maxsize = wordsArray[i].length();
}

}
System.out.println("Max sized word is " + maxWord + " with size " + maxsize);
}
}

 

Thulasiram
SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment