Page 496 - computer science (868) class 11
P. 496
20 StringTokenizer st=new StringTokenizer(str1);
21 int len1=0,len2=100;
22 while(st.hasMoreTokens())
23 {
24 wd=st.nextToken();
25 if(wd.length()>len1)
26 {
27 len1=wd.length();
28 large=wd;
29 }
30 if(wd.length() < len2)
31 {
32 len2=wd.length();
33 small=wd;
34 }
35 }//loop ends
36 System.out.println("Word with maximum length="+large);
37 System.out.println("And it’s length="+large.length());
38 System.out.println("Word with minimum length="+small);
39 System.out.println("And it’s length="+small.length());
40 }//methods ends
41 }//class ends
The output of the preceding program is as follows:
Enter a Sentence : The current stage of my book is final.
Given Sentence : The current stage of my book is final.
Word with maximum length=current
And it’s length=7
Word with minimum length=of
And it’s length=2
Variable Description
NAME TYPE DESCRIPTION
str1 String Sentence taken from the user
large String Stores the largest
small String Stores the smallest
len int Length of the word
wd String Word extracted from the sentence
i int Loop variable
494494 Touchpad Computer Science-XI

