-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterCounter.java
More file actions
27 lines (23 loc) · 918 Bytes
/
CharacterCounter.java
File metadata and controls
27 lines (23 loc) · 918 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
import java.util.Scanner;
public class CharacterCounter {
public static int count(char[] chars, char ch) {
return count(chars, ch, 0);
}
public static int count(char[] chars, char ch, int high) {
if (high == chars.length) return 0;
else if (chars[high] == ch) return 1 + count(chars, ch, high + 1);
else return count(chars, ch, high + 1);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter String : ");
String x = input.nextLine();
char[] list = x.toCharArray();
outer:
for (int i = 0; i < list.length; i++) {
if (String.valueOf(list[i]).equals(" ")) continue;
for (int j = 0; j < i; j++) if (list[j] == list[i]) continue outer;
System.out.printf("%s occurs %2d times \n", list[i], count(list, list[i]));
}
}
}