HJ2 计算某字符出现次数
法一
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = (sc.nextLine()).toLowerCase();
char[] ch = str1.toCharArray();
String str2 = (sc.nextLine()).toLowerCase();
char cr = str2.charAt(0);
int count = 0;
for (int i = 0 ; i < ch.length ; i++ ) {
if (ch[i] == cr) {
count++;
}
}
System.out.println(count);
}
}
}
法二
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = (sc.nextLine()).toLowerCase();
String str2 = (sc.nextLine()).toLowerCase();
String str3 = str1.replaceAll(str2, "");
int len = str1.length() - str3.length();
System.out.println(len);
}
}
}