Posts

Showing posts with the label Java Program to Find Largest word in a String

Java - Find Largest and Smallest word in a String

Image
In this section, we will show you how to find longest word in as given String in Java . 1.  Using For loop 2. Using  Java 8 Streams Find Largest Word: Using For loop public class Main { public static void main ( String [] args) { String inputString = "Java is an awesome programming language" ; String [] strArray = inputString .split( " " ); String maxlengthWord = "" ; for ( int i = 0 ; i < strArray . length ; i ++){ if ( strArray [ i ].length() > maxlengthWord .length()){ maxlengthWord = strArray [ i ]; } } System . out .println( maxlengthWord ); } } Here the logic is simple, First, declare the string as a string literal. Using the split() method, split the string based on whitespace. It returns an array of strings. Declare an empty string; later, we use it to accumulate the longest word. Iterate over the string array and check whether the length of the [