This is the Java Program to Find the Largest and Smallest Word
Given a string of characters, find the largest and the smallest word if two or more words are the largest or smallest then display the one that occurs first in the string.
Example:
String x = “We belong to Russia”
Output = We, belong
“We” and “to” both are the smallest words since “We” is encountered first, it comes in the output, same is with “belong” and “Russia”.
Split the given string into various substrings, whenever space is encountered. Iterate through these substrings to find the first largest and smallest word.
Here is the source code of the Java Program to Find the Largest and Smallest Word. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
// Java program to Find the Largest and Smallest Word.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class LargestAndSmallestWord {
// Method to split the string and find the largest and smallest word
static void printLargestAndSmallestWord(String str){
String[] arr=str.split(" ");
int i=0;
int maxlength,minlength;
maxlength=Integer.MIN_VALUE;
minlength=Integer.MAX_VALUE;
String largest,smallest;
largest = smallest = "";
for(i=0;i<arr.length;i++){
if(arr[i].length() < minlength){
smallest=arr[i];
minlength=arr[i].length();
}
if(arr[i].length() > maxlength) {
largest=arr[i];
maxlength=arr[i].length();
}
}
System.out.println("The largest and smallest word is \"" + largest +
"\" and \"" + smallest + "\"");
}
// Main function to read the string
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the text string");
String str;
try{
str=br.readLine();
}
catch(Exception e){
System.out.println("Error reading input");
return;
}
printLargestAndSmallestWord(str);
}
}
1. In function printLargestAndSmallestWord(), the string is split into various tokens using s.split(” “) statement.
2. The minlength and maxlength variables are initialized to Integer.MAX_VALUE and Integer.MIN_VALUE, respectively.
3. The loop for(i=0;i<arr.length;i++) is used to iterate through the array of tokens.
4. The condition if(arr[i].length() < minlength) checks if the current token is the smallest, if it is the minlength and smallest variables are updated accordingly.
5. The condition if(arr[i].length() > maxlength) checks if the current token is the largest, if it is the maxlength and largest variables are updated accordingly.
Time Complexity: O(n) where n is the length of the string.
Case 1 (Simple Test Case): Enter the text string I am IronMan The largest and smallest word is "IronMan" and "I" Case 2 (Simple Test Case - another example): Enter the text string We belong to Russia The largest and smallest word is "belong" and "We"
Sanfoundry Global Education & Learning Series – Java Programs.
- Apply for Java Internship
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check Programming Books
- Check Java Books