-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion88_CatsAndDogs.java
More file actions
48 lines (36 loc) · 979 Bytes
/
Question88_CatsAndDogs.java
File metadata and controls
48 lines (36 loc) · 979 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
43
44
45
46
47
48
package week6_String;
import java.util.Scanner;
public class Question88_CatsAndDogs {
public static void main(String[] args) {
// olcay // Jul 7, 2020
/*Print true if the string "cat" and "dog" appear the same number of times in the given string word.
Example:
input: catdog
output: true
Example:
input: catcat
output: false
Example:
input: cat-cheetah-dog-cat
output: false
*/
System.out.println("write the sentence:");
Scanner scan = new Scanner(System.in);
int countOfCats = 0;
int countOfDogs = 0;
String word = scan.next();
for(int i=0; i<word.length()-2; i++) {
if(word.substring(i, i+3).equals("cat")) {
countOfCats++;
}
if(word.substring(i, i+3).equals("dog")) {
countOfDogs++;
}
}
if(countOfCats==countOfDogs) {
System.out.println(true);
}else {
System.out.println(false);
}
}
}