Java Program to Find the Frequency of Characters in a String

In this Java program, you’ll learn how to Find the Frequency of Characters in a String using the Java programming language

How Find the Frequency of Characters in a String using JAVA? 

Example 1: 

RUN CODE SNIPPET
public class Main { 
    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 = 4

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Java program, we will learn how to find the GCD of two numbers using Java.  GCD (Greatest Common...
  • Java
  • December 3, 2024
In this Java Program, you’ll learn how to swap two numbers using the Java programming language.  How to Swap Two...
  • Java
  • December 2, 2024
In this Java program , we will learn how to Find Largest Element in an Array in your Java program.   How to Find Largest Element...
  • Java
  • December 2, 2024