×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Find the highest and the lowest character in a word through java
Find Longest word in a sentence through java - Java
40419    Arnab De    09/09/2018

Write a program to input a sentence and print the numbers of characters found in the longest word of the given sentence.

At first import the util pacakage for creating the object of the scanner class. Now create an object (sc) of Scanner class. Read the sentence using nextLine() method of Scanner class. Now split the sentence into words using split() method and store into a string array. Now set a variable max and initialized to 0. Now check all the words and compare the length of the word and max variables and store the maximum length of the word and print it.

softetechnologies
import java.util.*;
public class longestWord
{
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a Setance : ");
        String str=sc.nextLine();
        String []words=str.split(" ");
        int max=0;
		String res="";
        for(int i=0;imax)
            {
                max=words[i].length();
				res=words[i];
            }
        }
        System.out.print("Maximum Length of the words is : " + max);
		System.out.print("Maximum Length words is : " + res);
    }
}
softetechnologies

Enter a Setance :
People prefer electric vehicles over conventional ones
Maximum Length of the words is : 12
Maximum Length words is : conventional

Find the highest and the lowest character in a word through java
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Rabindranath Tagore
You can't cross the sea merely by standing and staring at the water.
Rabindranath Tagore
1139
57.24
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     43527
01/01/2018     36477
25/06/2018     35475
28/06/2017     34547
02/08/2017     32975
01/08/2017     27458
06/07/2017     27205
15/05/2017     26840
14/07/2017     22455
21/04/2018     21107
softetechnologies