Page 237 - computer science (868) class 11
P. 237
6 Scanner sc= new Scanner(System.in);
7 String str;
8 int i, l, cw=0, ca=0, cd=0;
9 change color;
10 System.out.print("Enter a sentence: ");
11 str=sc.nextLine();
12 l=str.length();
13 for(i=0; i<l; i++)
14 {
15 ch=str.charAt(i);
16 if(Character.isWhitespace(ch))
17 {
18 cw++;
19 }
20 if(Character.isLetter(ch))
21 {
22 ca++;
23 }
24 if(Character.isDigit(ch))
25 {
26 cd++;
27 }
28 }
29 System.out.println("Number of words: "+(cw+1));
30 System.out.println("Number of alphabets: " + ca);
31 System.out.println("Number of digits: "+cd);
32 }
33 }
The output of the preceding program is as follows:
Enter a sentence: hello world i am back
Number of words: 5
Number of alphabets: 17
Number of digits: 0
235
Strings 235

