Program to Search Largest & Smallest Word in a String
In this Program, we will be taking the input sentence and after taking the input sentence we will search for the largest and smallest word . There are various ways to write code for this topic, but I have found the easiest one for you. Just go through the algorithm you will understand the logic behind this code.
Note: For Mobile User, Please view it in Desktop mode
Algorithm/Program Explanation
- Start
- static public void Search(String string)// defining search method
- Define and Initialize the following variables
- String str= string+””;//for extracting last word
- char ch= ‘ ‘;
- int length=str.length();// Storing length of str
- int count=0,minimum=length,maximum=0;
- String smallest=””, largest=””,word=””;
- Define and Initialize For loop
- for(int i=0;i<length;i++)
- ch=str.charAt(i)
- Check for Condition
- if(ch!=’ ‘) if character which is not equal to space then form words
- word+=ch;//here we are forming words from character excluding spaces
- else
- count=word.length();
- Check for Condition
- if(count< minimum)//checking for minimum length
- minimum=count
- smallest=word;
- if(count> maximum)//Checking for maximum length
- maximum=count
- largest=word;
- word=””// emptying temporary variable to store new words
- print smallest word
- print largest word
- end
Program to Search Largest & Smallest Word in a String:
/*
Java Program To search largesr and smallest word in the sgiven string */
import java.util.*;
public class LargestSmallestword{
public static void main(String args[]){
Search("My Name is NotesHacker and it is best Website for Learning");
}
static public void Search(String string)// defining search method
{
String str= string+"";//for extracting last word
char ch= ' ';
int length=str.length();
int count=0;int minimum=length,maximum=0;
String smallest="", largest="",word="";
for(int i=0;i<length;i++){
ch=str.charAt(i);
if(ch!=' '){
word+=ch;//here we are forming words from character excluding spaces
}
else{
count=word.length();
if(count< minimum)//checking for minimum length
{
minimum=count;
smallest=word;
}
if(count> maximum){
maximum=count;
largest=word;
}
word="";// emptying variable for storing next word
}
}
System.out.println("Smallest Word=" + smallest + " length=" + minimum);
System.out.println("Largest Word=" + largest + " length=" + maximum);
}
}
Output:
Smallest Word=My length=2
Largest Word=NotesHacker length=11
Related Topics
- Arithmetic Operations Java Program
- Java program to perform Logical and Relational operations
- Java program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with Explanation
- Java Program to find Min,Max Element in an Array
- Java Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an array
- Program to find common elements between two arrays
- Java Program to Add/Insert specific Element to an Array
- Java Program to delete specific element from an Array
- Program to calculate frequency of each element in Array
- Java Program to find Even and Odd Numbers in the arrays
- Java Program to search Given Number in an Array
- Java Program to Add or Sub Two Matrices
- Java Program to Multiply two or More Matrices
- Java Program to Print Array in Reverse Order
- Java Program To Transpose Given Matrix
- Java Programs List- Java Programs Examples With Output