Finding the largest word in a sentence with Java

Day(15/30) Today I practiced a simple yet logical Java problem: Find the largest word in a sentence. Here’s the logic in simple steps 1 Split the sentence into words using split(" ") 2 Loop through each word using index-based for loop 3 Compare each word’s length 4 Keep updating the longest word String sentence = "Java is a powerful programming language"; String[] words = sentence.split(" "); String largestWord = ""; int maxLength = 0; for (int i = 0; i < words.length; i++) { String word = words[i]; if (word.length() > maxLength) { maxLength = word.length(); largestWord = word; } } System.out.println("Largest Word: " + largestWord); This small exercise helped me understand string traversal and comparison in Java better. Sometimes, even simple problems teach powerful concepts like loops, arrays, and string handling. #Java #Programming #BCA #LearningInPublic #90DaysOfCode #CodingJourney

To view or add a comment, sign in

Explore content categories