Java Program to find the occurrence of characters in a string


February 6, 2022, Learn eTutorial
973

Here we are explaining how to write a java program to find out the number of occurrences of the characters in a string. So first we have to read the string from the user and we will call the function countChar() to find out the occurrence of the character.

How to read a string in java?

For reading string from the user in java, we can use nextLine() function in the java Scanner class. In this program, we have to read the string which the user wants to check into the string variable str. So we have to write the code as follows:

System.out.println("Enter a string : ");
System.out.println("Enter a string : ");
 

How to implement the java program to find the occurrence of the characters in a string?

First, we have to declare the class CCount .Then open the main function. Then read the string from the user into the variable str and call the function countChar(str) to find out the occurrence of each character in the string.

In the countChar(str) function,first declare a array c[] of size 256.The array c[] is used to keep the counter value of each character. Then read the length of the string into the variable len by using the function str.length().By using a for loop with the condition i

ALGORITHM

STEP 1: Declare the class CCount with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Read the string from the user into the variable str.

STEP 4: Call the function countChar(str).

FUNCTION static void countChar(String str)

STEP 1: Declare the array c[] as an integer of size 256.

STEP 2: Read the length of the string str into the variable len.

STEP 3: By using a for loop with the condition i

STEP 4: Create an array array[].

STEP 5: By using another for loop with the condition i

STEP 6:set array[i] to str.charAt(i).

STEP 7: Set flag to zero.

STEP 8: By using another for loop with the condition j<=i check str.charAt(i)=array[j] if true then incremnet the flag.

STEP 9: Increment i by one repeats step 8.

STEP 10: if flag =1 then displays the occurrence of character.

STEP 11: Repeat step 6.

Java Source Code

                                          import java.util.Scanner;
public class CCount {

    static void countChar(String str) {

        int c[] = new int[256];
        int len = str.length(); //length of the string
        for (int i = 0; i < len; i++)
            c[str.charAt(i)]++;
        char array[] = new char[str.length()]; //Create an array.
        for (int i = 0; i < len; i++) {
            array[i] = str.charAt(i);
            int flag = 0;
            for (int j = 0; j <= i; j++) {
                if (str.charAt(i) == array[j])
                    flag++;
            }

            if (flag == 1)
                System.out.println("Occurrence of char " + str.charAt(i) +
                    " in the String is:" + c[str.charAt(i)]);
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string : ");
        String str = sc.nextLine();
        sc.close();
        countChar(str);
    }
}
                                      

OUTPUT

OUTPUT 1
Enter a string : Butterfly
Occurrence of char B in the String is:1
Occurrence of char u in the String is:1
Occurrence of char t in the String is:2
Occurrence of char e in the String is:1
Occurrence of char r in the String is:1
Occurrence of char f in the String is:1
Occurrence of char l in the String is:1
Occurrence of char y in the String is:1