zoukankan      html  css  js  c++  java
  • 统计字符串中各类字符的个数

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

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

      }

    }

  • 相关阅读:
    Win7下安装iMac系统
    Windows平台cocos2d-x 3.0 android开发环境
    iOS Dev (50)用代码实现图片加圆角
    内部消息 微软中国云计算 内測Azure免费账号 赶紧申请 错过不再有
    android锁屏软件制作
    CF1019E Raining season
    各数据库系统独有函数
    其他函数
    日期时间函数
    字符串函数
  • 原文地址:https://www.cnblogs.com/changyinlu/p/4693835.html
Copyright © 2011-2022 走看看