题解 | #字符串分隔#

计算某字符出现次数

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


public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()) {
            String s1=sc.nextLine().toUpperCase();//接收查询字符串转换为全大写
            String s2=sc.nextLine().toUpperCase();//接收查询字符转换为大写
            System.out.println(Ones(s1,s2));
        }
    }
//编写函数
    public static int Ones(String s1, String s2) {
        char[] arr = s1.toCharArray();//获得查询字符串对应的字符数组
        int count =0;//计数器
        for (int i = 0; i < arr.length; i++) {
            if (s2.equals(arr[i]+"")){
                count++;
            }//遍历数组,比较
        }
        return count;//返回计数器
    }
}
全部评论

相关推荐

机械打工仔:有说的你怀疑一下就行了,直接问也太实诚了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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