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

计算某字符出现次数

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        //分别获取第一个字符串和第二个字符,由于不区分大小写,所以将两者都转换为小写;然后遍历第一个字符串,当其中的字符等于目标字符时,目标数+1
        Scanner sc = new Scanner(System.in);
        String str1 = sc.nextLine().toLowerCase();
        String str2 = sc.nextLine().toLowerCase();
        Character target = str2.charAt(0);

        int res = 0;
        
        for(int i=0;i<str1.length();i++){
            if(str1.charAt(i) == target){
                res ++;
            }
        }
        System.out.print(res);





    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务