题解 | #计算某字母出现次数#
计算某字母出现次数
http://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
import java.io.Serializable;
import java.util.Scanner;
/**
HJ2 计算某字母出现次数
输入:
ABCabc
A
输出:
2
/
public class Main {
public static void main(String[] args) {Scanner sc = new Scanner(System.in); String s = sc.nextLine(); char[] chars = s.toCharArray(); String s1 = sc.next(); char c = s1.charAt(0); char ch = Character.isLowerCase(c) ? c : Character.toLowerCase(c); int sum = 0; for(int i=0; i<s.length(); i++){ if(Character.isLowerCase(chars[i])){ if(ch==chars[i]){ sum++; } } if(Character.isUpperCase(chars[i])){ char c1 = Character.toLowerCase(chars[i]); if(ch==c1) sum++; } } System.out.println(sum);
}
}