Idea: simply using a multiple if structure can be solved.
CODE:
Import Java.util.Scanner;
public class character{
public static void Main (string[] args) {
System.out.println ("Please enter a string:");
Scanner ss = new Scanner (system.in);
String sc = ss.nextline ();
Char[] ch= Sc.tochararray (); String into a character array
int abccount = 0;
int numcount = 0;
int spacecount = 0;
int othercount = 0;
for (int i = 0;i<sc.length (); i++) {
if (ch[i]<= ' 9 ' &&ch[i]>= ' 0 ') {
numcount++;
}else if ((ch[i]<= ' z ' &&ch[i]>= ' a ') | | (ch[i]<= ' Z ' &&ch[i]>= ' A ')) {
abccount++;
}else if (ch[i]== ") {
spacecount++;
}else{
othercount++;
}
}
System.out.println ("Number of Numbers:" +numcount);
System.out.println ("The number of letters is:" +abccount);
System.out.println ("The number of spaces is:" +spacecount);
System.out.println ("Number of other characters:" +othercount);
}
}
Enter a string of any length to count the number of letters, numbers, spaces, and other characters.