题解 | #统计字符#

统计字符

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());
        }
    }
全部评论

相关推荐

01-14 19:01
吉首大学 Java
黑皮白袜臭脚体育生:加个项目吧,一般需要两个项目一业务一轮子呢,简历统一按使用了什么技术实现了什么功能解决了什么问题或提升了什么性能指标来写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务