题解 | #计算某字符出现次数#

计算某字符出现次数

https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        
        String s = "";
        String a = "";
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            if(s == ""){
                s = in.nextLine();
            } else {
                a = in.nextLine();
            }
        }

        result(s, a);
    }


    private static void result(String s, String a){

        int count = 0;
        int len = s.length();
        char c1 = a.charAt(0);
        boolean num = c1 >= '0' && c1 <= '9';
        int n = 'a' - 'A';
        for(int i=0;i<len;i++){
            char c = s.charAt(i);
            if(c == c1){
                count +=1;
                continue;
            } else{
                if(num){
                    continue;
                }
            }
            if(c - c1 == n || c1 -c == n){
                count +=1;
            }
        }

        System.out.print(count);
    }

}

#努力刷题下#
雪域灰灰刷题笔记 文章被收录于专栏

雪域灰灰刷题笔记

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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