题目
Description
给定原子串和目标子串,要你求得目标子串在原子串当中出现的次数。
Input
多组测试数据,每组测试数据第一行是原子串,第二行是目标子串
子串长度不会超过100.
Output
输出目标子串在原子串当中出现的次数。
Sample Input
abc123abc
abc
aabcCdeAbcAbcdeccde
cde
Sample Output
2
2
代码块
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cn = new Scanner(System.in);
while (cn.hasNext()) {
String str = cn.next();
String str1 = cn.next();
int length = str.length();
int length1 = str1.length();
int count = 0;
int t = str.indexOf(str1);
while (t >= 0 && length >= length1) {
t = str.indexOf(str1);
str = str.substring(t + length1 - 1);
count++;
}
System.out.println(count - 1);
}
}
}