题解 | #统计字符#

统计字符

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



public class test{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        if(sc.hasNext()){
            String str = sc.nextLine();
            //对所有字符进行替换,使之不存在
            String word = str.replaceAll("[a-zA-Z]", "");
            //得到所有字符的个数
            System.out.println(str.length()-word.length());
            //对所有数字进行替换,使之不存在
            String num = str.replaceAll("[0-9]", "");
            System.out.println(str.length()-num.length());
            //对所有空格进行替换,使之不存在
            String blank = str.replaceAll(" ", "");
            System.out.println(str.length()-blank.length());
            //其他的,通过总长度来进行减法
            System.out.println(word.length()+num.length()-2*str.length()+blank.length());
        }
    }
全部评论

相关推荐

面了100年面试不知...:太礼貌,还是
点赞 评论 收藏
分享
09-19 12:15
门头沟学院 Java
迷茫的大四🐶:这下是真的打牌了,我可以用感谢信和佬一起打牌吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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