A Java Program to find a lengthiest word.




Java Program to find a lengthiest word and the length of that word.


import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author lol ik
 */
public class killo {
    public static String getlongest(String[] array)
    {      
        int maxLength = 0;
       
    String longest = null;     
    for (String s : array) {         
    if (s.length() > maxLength)
    {               maxLength = s.length();              
    longest = s;           }   
}       return longest;
    }
    public static void main(String[] args)
    {
     System.out.println("Enter the String");
     Scanner in=new Scanner(System.in);
     String a=in.nextLine();
     String arr[]=a.split(" ");
     
     String longestString = getlongest(arr);
     System.out.format("longest string: '%s'\n", longestString);
     System.out.format("length of longest string : '%s'\n", longestString.length());
    
    }  




}
Posted by lol ik.

Comments

Popular Posts