-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterOccurrenceCounter.java
35 lines (28 loc) · 1.12 KB
/
CharacterOccurrenceCounter.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
import java.util.Scanner;
public class CharacterOccurrenceCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input string
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
// Input character to count
System.out.print("Enter a character to count: ");
char targetChar = scanner.next().charAt(0);
// Close the scanner
scanner.close();
// Call the function to count occurrences
int count = countOccurrences(inputString, targetChar);
// Display the result
System.out.println("The character '" + targetChar + "' occurs " + count + " times in the string.");
}
// Function to count occurrences of a character in a string
public static int countOccurrences(String inputString, char targetChar) {
int count = 0;
for (int i = 0; i < inputString.length(); i++) {
if (inputString.charAt(i) == targetChar) {
count++;
}
}
return count;
}
}