Strings
Java Program to Find Frequency of Character
A Java program to find the frequency of a character in a string.
Problem Description
Write a Java program to find the frequency of a specific character in a given string.
Code
public class Frequency {
public static void main(String[] args) {
String str = "This website is awesome.";
char ch = 'e';
int frequency = 0;
for(int i = 0; i < str.length(); i++) {
if(ch == str.charAt(i)) {
++frequency;
}
}
System.out.println("Frequency of " + ch + " = " + frequency);
}
}Output
Frequency of e = 4Explanation
- Loop: Iterate through each character of the string.
- Comparison: Check if the current character matches the target character.
- Counter: Increment if there's a match.
Java Program to Find String Length and Compare Strings
Learn to find string length, compare strings using equals(), and perform basic string operations in Java with practical examples and explanations.
Java Program to Count Vowels and Consonants
A Java program to count the number of vowels and consonants in a string.
