题解 | #统计字符#

统计字符

http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

import java.util.*;
import java.util.regex.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String s = in.nextLine();
            int englishNum = 0;
            int spaceNum = 0;
            int num = 0;
            int otherNum = 0;
            Pattern pattern = Pattern.compile("[a-zA-Z]*");
            Pattern pattern2 = Pattern.compile("[0-9]*");
            Pattern pattern3 = Pattern.compile("[ ]*");
            for(int i=0;i<s.length();i++){
                char c = s.charAt(i);
                if(pattern.matcher(String.valueOf(c)).matches()){
                    englishNum++;
                }else if(pattern2.matcher(String.valueOf(c)).matches()){
                    num++;
                }else if(pattern3.matcher(String.valueOf(c)).matches()){
                    spaceNum++;
                }else{
                    otherNum++;
                }
            }
            System.out.println(englishNum);
            System.out.println(spaceNum);
            System.out.println(num);
            System.out.println(otherNum);
        }
    }
}
全部评论

相关推荐

牛客60022193...:大厂都招前端,他们觉得AI能替代前端,可能他们公司吊打btaj吧
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务