-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSem2_T3_9.java
More file actions
72 lines (64 loc) · 2.02 KB
/
Sem2_T3_9.java
File metadata and controls
72 lines (64 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.io.*;
// Converting String into Character Array.
// WAP to display count of words, digits, alphabets, spaces, lines, characters of given file on Console.
public class Sem2_T3_9 {
static int wordCount(String s) {
String wc[] = s.split(" ");
return wc.length;
}
static int digitCount(String s) {
int count = 0;
char ch[] = s.toCharArray();
for (char c : ch) {
if (Character.isDigit(c)) {
count++;
}
}
return count;
}
static int alphabetCount(String s) {
int c = 0;
char ch[] = s.toCharArray();
for (char x : ch) {
if(Character.isLetter(x)) {
c++;
}
}
return c;
}
static int spacesCount(String s) {
int c = 0;
char ch[] = s.toCharArray();
for (char d : ch) {
if (Character.isWhitespace(d)) {
c++;
}
}
return c;
}
static int characterCount(String s) {
char ch[] = s.toCharArray();
return ch.length;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("mydata1.txt"));
String l = br.readLine();
int wc = 0, dc = 0, ac = 0, sc = 0, lc = 0, cc = 0;
while (l != null) {
lc++;
wc = wc + wordCount(l);
dc = dc + digitCount(l);
ac = ac + alphabetCount(l);
sc = sc + spacesCount(l);
cc = cc + characterCount(l);
l = br.readLine();
}
System.out.println("Line Count : " + lc);
System.out.println("Word Count : " + wc);
System.out.println("Digit Count : " + dc);
System.out.println("Alphabet Count : " + ac);
System.out.println("Spaces Count : " + sc);
System.out.println("Character Count : " + cc);
br.close();
}
}