import java.util.Scanner;
/* 计算字符串str2在字符串str1中出现的次数
* 例如:str1 = "aaaa",str2 = "aa",出现的次数记为两次
* */
public class JiSuanZiFuChuanChuXianCiShu {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str1 = scanner.nextLine().toLowerCase();
String str2 = scanner.nextLine().toLowerCase();
scanner.close();
int count = 0;
int temp = 0;
int j = 0;
if (str1.contains(str2) == false)
System.out.print(0);
else {
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) == str2.charAt(j) && j < str2.length()) {
temp++;
j++;
if (temp == str2.length()) {
count++;
temp = 0;
j = 0;
}
} else {
temp = 0;
j = 0;
}
}
}
System.out.print(count);
}
}
计算一个字符串在另外一个字符串中出现的次数
最新推荐文章于 2023-11-12 22:19:54 发布