-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathSymbolOccurrences.java
More file actions
43 lines (36 loc) · 1.31 KB
/
SymbolOccurrences.java
File metadata and controls
43 lines (36 loc) · 1.31 KB
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
/*
* File: SymbolOccurrences.java
* ---------------------
* ამოცანის პირობა:
* მომხმარებელს კონსოლიდან შეჰყავს ტექსტი და სიმბოლო,
* თქვენმა პროგრამამ უნდა იპოვოს რამდენჯერ გვხვდება მოცემულ ტექსტში მოცემული სიმბოლო.
*/
import acm.program.ConsoleProgram;
public class SymbolOccurrences extends ConsoleProgram {
public void run() {
// input
String text = readLine("text: ");
char symbol = readChar();
int res = count(text, symbol);
// print result
println("Total: " + res);
}
/* Asks user to prompt exactly one symbol */
private char readChar() {
String symbol = readLine("symbol: ");
while (symbol.length() != 1) {
symbol = readLine("Please enter one symbol: ");
}
return symbol.charAt(0);
}
/* Counts symbol occurrences */
private int count(String text, char ch) {
int cnt = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == ch) {
cnt++;
}
}
return cnt;
}
}