Page 378 - computer science (868) class 11
P. 378
3 {
4 String s;
5 int len;
6 void read() // accept sentence
7 {
8 Scanner sc = new Scanner(System.in);
9 System.out.println("Enter a sentence ending with fullstop");
10 s = sc.nextLine();
11 len=s.length();
12 }
13
14 int count(int l)
15 { if(l==len) // end of sentence base case
16 return 0;
17 // checking for word seperators
18 else if(s.charAt(l)==' '||s.charAt(l)==','||s.charAt(l)=='?'
||s.charAt(l)=='.')
19 return 1+count(l+1); // count increase by 1
20 else
21 return count(l+1);
22 }
23
24 void show()
25 {
26 int c = count(0);
27 System.out.println("Number of words = "+c); // display
28 }
29
30 public static void main() // main
31 {
32 Word ob = new Word();
33 ob.read();
34 ob.show();
35 }
36 }
376376 Touchpad Computer Science-XI

