首页 > 其他 > 详细

统计字符串中各类字符的个数

时间:2015-08-01 12:52:56      阅读:180      评论:0      收藏:0      [点我收藏+]

//输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

public class lianxi07 {
  public static void main(String[] args) {
    int digital = 0;
    int character = 0;
    int other = 0;
    int blank = 0;
       char[] ch = null;
       Scanner sc = new Scanner(System.in);
       String s = sc.nextLine();
       ch = s.toCharArray();
       for(int i=0; i<ch.length; i++) {
          if(ch >= ‘0‘ && ch <= ‘9‘) {
             digital ++;
          } else if((ch >= ‘a‘ && ch <= ‘z‘) || ch > ‘A‘ && ch <= ‘Z‘) {
             character ++;
          } else if(ch == ‘ ‘) {
             blank ++;
          } else {
             other ++;
          }
        }
       System.out.println("数字个数: " + digital);
       System.out.println("英文字母个数: " + character);
       System.out.println("空格个数: " + blank);
       System.out.println("其他字符个数:" + other );

  }

}

 

统计字符串中各类字符的个数

原文:http://www.cnblogs.com/changyinlu/p/4693835.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!