-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindCharacters.java
47 lines (39 loc) · 947 Bytes
/
FindCharacters.java
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
43
44
45
46
47
import java.io.IOException;
public class FindCharacters {
public static void main(String[] args) {
String text = "To be or not to be, that is the question;"
+"Whether 'this nobler in the mind to suffer"
+" the slings and arrows of outragerous fortune,"
+" or to take arms against a sea of troubles,"
+" and by opposing and them?";
int andCount = 0;
int theCount = 0;
int index = -1;
String andStr = "and";
String theStr = "the";
index = text.indexOf(andStr);
while(index >= 0)
{
++andCount;
index += andStr.length();
index = text.indexOf(andStr, index);
}
index = text.lastIndexOf(theStr);
while(index >= 0)
{
++theCount;
index -= theStr.length();
index = text.lastIndexOf(theStr, index);
}
System.out.println("The text contains " + andCount + " ands\n"
+ "The text contains " + theCount + " thes");
try
{
System.in.read();
}
catch (IOException e)
{
return;
}
}
}