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

计算某字符出现次数

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

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

	Scanner sc = new Scanner(System.in);
	while (sc.hasNextLine()) {
		// 第一行输入一个由字母和数字以及空格组成的字符串
		String str = sc.nextLine().trim().replaceAll("\\W+", "").toUpperCase();
		// 第二行输入一个字符
		String target = sc.nextLine().trim().toUpperCase();
		// 消除str中非target字符

// System.out.println(str); // System.out.println(target);

		int count = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == target.charAt(0)) {
				count++;
			}
		}

		System.out.println(count);

	}
	sc.close();

}

}

全部评论

相关推荐

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