题解 | #统计字符串中字母出现次数#

统计字符串中字母出现次数

https://www.nowcoder.com/practice/83350872bdb5406fa706895d5efb1c55

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String string = "H e l l o ! n o w c o d e r";
        Scanner scanner= new Scanner(System.in);
        String word = scanner.next();
        scanner.close();
        System.out.println(check(string, word));
    }

    public static int check(String str, String word) {

        //write your code here......
        // 记录字母在字符串中出现次数
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            String ch = String.valueOf(str.charAt(i));
            if (word.equals(ch)) {
                num++;
            }
        }
        return num;
    }
}

全部评论

相关推荐

夏日狂想曲:连体婴是这样的,不过国内还有上四休三的公司?
点赞 评论 收藏
分享
牛客54175811...:今年对双非很难。1、争取一段大厂实习经历,2、狂磕八股,3、再跑个难度提升的项目。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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