Java Program to Find the Frequency of Character in a String

Java Program to Find the Frequency of Character in a String

What is String in Java ?

In Java, a String is a sequence of characters.Strings are often used to store and manipulate text data, such as names, addresses, and other information.

Strings are immutable, which means that once a string is created, its value cannot be changed. Instead, operations that appear to modify a string actually return a new string object with the modified value.

Steps to Find the Frequency of Character in a String :

  • These are the Steps :
    1. Create a string variable to store the string .
    2. Initialize a variable to store the count of the character that you want to find.
    3. Use a loop to iterate over each character in the string.
    4. Use an if statement to check if the current character is equal to the character you want to find.
    5. If the current character is equal to the target character, increment the count.
    6. Continue the loop until all characters in the string have been processed.

Code example for the above algorithm :

 public static int findFrequency(String str, char c) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == c) {
                count++;
            }
        }
        return count;
    }

Example 1:

Run

public class Main {
    public static void main(String[] args) {
        String str = "Hello, world!";
        char c = 'o';
        int frequency = findFrequency(str, c);
        System.out.println("The frequency of '" + c + "' in the string '" + str + "' is: " + frequency);
    }
 
    public static int findFrequency(String str, char c) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == c) {
                count++;
            }
        }
        return count;
    }
}

Output :

The frequency of 'o' in the string 'Hello, world!' is: 2

Explanation : 

In this example, the findFrequency function implements the steps to find the frequency of a character in a string. The main method creates a string “Hello, world!” and a character ‘o’ and calls the findFrequency function to find the frequency of the character ‘o’ in the string. The result is then printed to the console. The findFrequency function uses a loop to iterate over each character in the string and an if statement to check if the current character is equal to the target character. The frequency of the character is returned as the final result.

Example 2:

Run

public class Main {
    public static void main(String[] args) {
        String str = "Hello, world!";
        char c = 'o';
        int frequency = findFrequency(str, c);
        System.out.println("The frequency of '" + c + "' in the string '" + str + "' is: " + frequency);
    }
 
    public static int findFrequency(String str, char c) {
        int count = 0;
        int i = 0;
        while (i < str.length()) {
            if (str.charAt(i) == c) {
                count++;
            }
            i++;
        }
        return count;
    }
}

Output :

The frequency of 'o' in the string 'Hello, world!' is: 2

Explanation :  

In this example, the findFrequency function uses a while loop instead of a for loop to iterate over each character in the string. The while loop continues as long as the variable i is less than the length of the string. Inside the loop, the if statement checks if the current character is equal to the target character, and if so, the count variable is incremented. The frequency of the character is returned as the final result. The rest of the code is the same as the previous example.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription