-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice49.java
More file actions
34 lines (30 loc) · 928 Bytes
/
Copy pathPractice49.java
File metadata and controls
34 lines (30 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package fiftypratice;
import java.util.Scanner;
/**
* 题目:计算字符串中子串出现的次数
* */
public class Practice49 {
public static void main(String[] args) {
System.out.println("请输入一个字符串:");
Scanner s=new Scanner(System.in);
String str=s.nextLine();
System.out.println("请输入子串:");
String str2=s.nextLine();
int count=0;
if(str.equals("")||str2.equals("")){
System.out.println("你没有输入字符串!");
System.exit(0);
}
else {
for(int i=0;i<=(str.length()-str2.length());i++){
if(str2.equals(str.substring(i,str2.length()+i))){
count++;
}
}
}
System.out.println("出现的次数:"+count);
if(s!=null){
s.close();
}
}
}