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

计算某字符出现次数

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();//字符串
        char[] chars = new char[1000];
/ *  字符串转字符数组
 *  dst.getChars()
 *  getChars(int srcBegin,int srcEnd,char[]dst,int dstBegin)
 *  将下标从srcBegin到srcEnd-1的子串复制到字符数组dst中下标从dstBegin开始的位置
 *  例子:Char[] dst={'j','a','v','a','1','3','0','1'};
 *       "CS3720".getChars(2,6,dst,4);
 *       dst就变成了{'j','a','v','a','3','7','2','0'}。
 */
        str.getChars(0,str.length(),chars,0);

        char chr = in.next().charAt(0);//字符


        int num = 0;
        for(int i=0;i<str.length();i++){
//忽略大小写
            if(String.valueOf(chars[i]).equalsIgnoreCase(String.valueOf(chr))){

                num++;

            }
        }
        System.out.println(num);
    }   
 }

#我的实习求职记录#
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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