-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem4.java
More file actions
42 lines (38 loc) · 919 Bytes
/
Problem4.java
File metadata and controls
42 lines (38 loc) · 919 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
35
36
37
38
39
40
41
42
import java.util.Scanner;
public class Problem4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String str = new String();
int count = 0;
int i = 0;
System.out.print("Enter a text: ");
String text = in.nextLine();
while(true) {
System.out.print("Enter a string: ");
str = in.nextLine();
if(str.length() == 0) {
System.out.println("You must enter a string.");
continue;
}
else {
char chStr[] = str.toCharArray();
char chText[] = text.toCharArray();
while(i <= text.length()-str.length()) {
for(int j = 0; j < str.length(); j++) {
if(chStr[j] != chText[i+j])
break;
if(j == str.length()-1) {
count++;
}
}
i++;
}
System.out.print("There are " + count +
" instances of " + "\"" + str +"\"" + ".\n");
break;
}
}
in.close();
}
}