-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCR2.java
More file actions
36 lines (31 loc) · 1022 Bytes
/
CR2.java
File metadata and controls
36 lines (31 loc) · 1022 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
import java.util.ArrayList;
class CR2 {
public static void main(String[] args) {
ArrayList<String> list10 = new ArrayList<String>();
list10.add(" orange");
list10.add(" Orange ");
list10.add("oRANGE ");
list10.add("orange ");
list10.add("apple");
int count10 = countOccurrences(list10, " orANge");
System.out.println("Test Case 10: 'orange' (case insensitive with various spaces) count = " + count10);
ArrayList<String> list7 = new ArrayList<String>();
list7.add("pear");
list7.add(" pear ");
list7.add("peach");
int count7 = countOccurrences(list7, " pear ");
System.out.println("Test Case 7: 'pear' (with input spaces) count = " + count7);
}
public static int countOccurrences(ArrayList<String> list, String word) {
if (list.isEmpty()){
return 0;
}
int count = 0;
for (int i = 0; i < list.size(); i++) {
if (word.trim().equalsIgnoreCase(list.get(i).trim())) {
count++;
}
}
return count;
}
}