Wednesday, 15 July 2015

Java Programs Made Easy

Q)...find the max length-word in a given string and return the max-length word. if two words are of same length return the first occuring word of max-length
input:"hello how are you aaaaa"
output:hello(length-5)



  1. package Set1;
  2. import java.util.*;
  3. public class ClassSet14 {
  4.  public static String lengthiestString(String s1){
  5.   int max=0;
  6.      String s2=null;
  7.         StringTokenizer t=new StringTokenizer(s1," ");
  8.         while(t.hasMoreTokens()){
  9.          s1=t.nextToken();
  10.          int n=s1.length();
  11.          if(n>max){
  12.           max=n;
  13.           s2=s1;  }
  14.          }
  15.         return s2;
  16.  }
  17.  public static void main(String[] args) {
  18.   Scanner s=new Scanner(System.in);
  19.   System.out.println("enter the String:");
  20.   String s1=s.nextLine();
  21.   System.out.println("the lengthiest string is:"+lengthiestString(s1));
  22.   }
  23. }