题解 | #统计字符串中字母出现次数#
统计字符串中字母出现次数
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......
/** 转换成StringBuilder 遍历数组,对比并记录出现次数 */
int result = 0;
StringBuilder sb = new StringBuilder(str);
for(int i = 0; i < sb.length(); i++){
if(sb.charAt(i) == word.charAt(0)){
result++;
}
}
return result;
}
}
查看14道真题和解析