---
title: "Find the largest and shortest word in a sentence using java"  
description: "In this blog I am providing you the code for finding the largest and shortest word in a sentence using java.  import java.io.*;  class Short_long_word"  
author: "Vijay Shukla"  
published: 2013-10-04  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/599/find-the-largest-and-shortest-word-in-a-sentence-using-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Find the largest and shortest word in a sentence using java

In this [blog](https://www.mindstick.com/articles/12705/myths-and-misconception-about-blog) I am providing you the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) for finding the largest and shortest [word](https://www.mindstick.com/forum/305/read-word-file) in a sentence using java.

\

```
import java.io.*;
class Short_long_word{
public static void main(String [] args)throws IOException    {        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any sentence : "); //inputting the sentence
        String val=br.readLine();                String s = val.replaceAll("[-+.^:,?]"," ");
                s = s.replaceAll("( )+", " ");
        s=s+" "; //adding a space at the end, to extract the last word also        int len=s.length(); //finding the length of the sentence        String x="",maxw="",minw="";
        char ch;                int p,maxl=0,minl=len;
        for(int i=0;i<len;i++)        {            ch=s.charAt(i); //extracting characters of the string one at a time
                   // System.out.println(ch);
            if(ch!=' ')            {                x=x+ch; //adding characters to form word if character is not space
            }             else            {                p=x.length();                if(p<minl) //checking for minimum length                {                    minl=p;                    minw=x;                }                if(p>maxl) //checking for maximum length                {                    maxl=p;                    maxw=x;                }                x=""; //emptying the temporary variable to store next word            }        }        System.out.println("Shortest word = "+minw+"\nLength = "+minl);        System.out.println("Longest word = "+maxw+"\nLength = "+maxl);    }
}
```

---

Original Source: https://www.mindstick.com/blog/599/find-the-largest-and-shortest-word-in-a-sentence-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
