Page 377 - computer science (868) class 11
P. 377
24 }
25
26 public static void main(int x) // main
27 {
28 Pattern ob = new Pattern();
29 ob.read(x);
30 ob.print(1, 1);
31 }
32 }
When you execute the preceding program, the Method Call dialog box appears:
The output of the preceding program is as follows:
12345
2345
345
45
5
12.3.2 Recursive Methods on String
While defining a recursive method to handle strings, the base case is reached when the index position of the string
becomes equal to the length of the string or -1 or null.
Program 9 A class called Word is defined to count number of words in a sentence. The words in a
sentence can be separated by space only. The class description is given below:
Class name : Word
Data Members
String s : To store a sentence
Member Methods
void read (int l) : Accepts any sentence
int count(String s) : Using recursive technique, counts and returns number of words in
the sentence
void show() : Calls count(String s) and prints word count
static void main() : Creates object and calls other methods
1 import java.util.*;
2 class Word
375
Recursion 375

