-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount.java
More file actions
32 lines (27 loc) · 1.02 KB
/
Count.java
File metadata and controls
32 lines (27 loc) · 1.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
import java.util.Scanner;
public class Count {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
int charCount = 0;
int wordCount = 0;
int specialCharCount = 0;
int spaceCount = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isLetter(str.charAt(i))) {
charCount++;
} else if (Character.isWhitespace(str.charAt(i))) {
spaceCount++;
} else {
specialCharCount++;
}
}
String[] words = str.split("\\s+");
wordCount = words.length;
System.out.println("Number of characters: " + charCount);
System.out.println("Number of words: " + wordCount);
System.out.println("Number of special characters: " + specialCharCount);
System.out.println("Number of white spaces: " + spaceCount);
}
}