Enter a string of any length to count the number of letters, numbers, spaces, and other characters ., String Space

Source: Internet
Author: User

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 );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.