-
Notifications
You must be signed in to change notification settings - Fork 0
/
characterCount.java
42 lines (40 loc) · 1.64 KB
/
characterCount.java
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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class characterCount {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = in.nextLine();
String[] words = input.split(" ");
int count = words.length;
int characterCount = input.replace(" ", "").length();
int upperCaseCount = 0;
int lowerCaseCount = 0;
for (char c : input.toCharArray()) {
if (Character.isUpperCase(c)) {
upperCaseCount++;
} else if (Character.isLowerCase(c)) {
lowerCaseCount++;
}
}
System.out.println("Number of words: " + count);
System.out.println("Number of characters: " + characterCount);
System.out.println("Number of uppercase letters: " + upperCaseCount);
System.out.println("Number of lowercase letters: " + lowerCaseCount);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
writer.write("String entered: " + input + "\n");
writer.write("Number of words: " + count + "\n");
writer.write("Number of characters: " + characterCount + "\n");
writer.write("Number of uppercase letters: " + upperCaseCount + "\n");
writer.write("Number of lowercase letters: " + lowerCaseCount + "\n");
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
in.close();
}
}