Page 406 - Cs_withBlue_J_C11_Flipbook
P. 406
21 {
22 p1 = s.indexOf(' ');
23 p2 = s.indexOf(' ', p1+1);
24 s1 = s.substring(p1+1, p2); // extracting each word and printing it
25 if(s1.length()>wrd.length()) // finding longest word
26 wrd = s1;
27 return findlong(s.substring(p2)); // recursive case
28 }
29 }
30
31 void show()
32 {
33 String w = findlong(sen); // method calling and printing longest word
34 System.out.println("Longest word = "+w);
35 }
36
37 public static void main()
38 {
39 Longword ob = new Longword();
40 ob.read();
41 ob.show();
42 }
43 }
The output of the preceding program is as follows:
Output 1:
Enter a sentence ending with fullstop
Whale is the biggest mammal on Earth.
Longest word = biggest
Output 2:
Enter a sentence ending with fullstop
The smallest mammal in the world is the bumblebee bat.
Longest word = bumblebee
13.3.3 Recursive Methods on Array
In the case of an array, the base case will be reached at the end of the array, that is, when the index position becomes
equal to the length of the array.
404404 Touchpad Computer Science-XI

