Enter a string of any length to count the number of letters, numbers, spaces, and other characters ., String Space
Idea: simply use a multi-if structure to solve the problem.
CODE:
Import java. util. collections;
Public class Character {
Public static void main (String [] args ){
System. out. println ("enter a string :");
Using ss = new using (System. in );
String SC = ss. nextLine ();
Char [] ch = SC. toCharArray (); // the string is converted 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:" + numcount );
System. out. println ("the number of letters is:" + abccount );
System. out. println ("number of spaces:" + spacecount );
System. out. println ("other characters:" + othercount );
}
}