Classify Characters
Completed
Read a line and print the number of letters, digits, spaces and other characters, as letters=L digits=D spaces=S other=O.
Input
One line.
Output
One line.
Example
Input
Hello World 2026!
Output
letters=10 digits=4 spaces=2 other=1
Editor
Java
Hints
toCharArray() lets you loop with for-each.
Character.isLetter and Character.isDigit classify a char.
Solution explanation
The Character class has many static helpers for testing and converting characters.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int letters = 0, digits = 0, spaces = 0, other = 0;
for (char c : s.toCharArray()) {
if (Character.isLetter(c)) {
letters++;
} else if (Character.isDigit(c)) {
digits++;
} else if (c == ' ') {
spaces++;
} else {
other++;
}
}
System.out.println("letters=" + letters + " digits=" + digits + " spaces=" + spaces + " other=" + other);
}
}