-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterFrequency2.java
42 lines (31 loc) · 1.11 KB
/
CharacterFrequency2.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
36
37
38
39
40
41
42
// EXPERIMENT 2
/* Write a Java Program to find the frequency of a given character in a string. */
import java.util.Scanner;
public class CharacterFrequency2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Taking input from the user
System.out.print("Enter a string: ");
String str = sc.nextLine();
System.out.print("Enter the character to find its frequency: ");
char ch = sc.next().charAt(0); // Reads a single character input from the user
// Initialize the frequency counter
int frequency = 0;
// Loop through the string to count occurrences of the given character
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
frequency++;
}
}
// Print the result
System.out.println("Frequency of '" + ch + "' in the string is: " + frequency);
// Closing the scanner
sc.close();
}
}
//SAMPLE OUTPUT
/*
Enter a string: programming
Enter the character to find its frequency: g
Frequency of 'g' in the string is: 2
*/