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

Source: Internet
Author: User

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.

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.