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

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

http://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) {

      	// 使用字符数组外加foreach遍历一遍后得到结果
        //write your code here......
        char[] ch = str.toCharArray();
        int counts = 0;
        for (char c : ch) if (Character.toString(c).equals(word)) counts++;
        return counts;
        
    }
}
全部评论

相关推荐

评论
7
收藏
分享

创作者周榜

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