Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaJava program to count the occurrences of each character

Java program to count the occurrences of each character

Write a Java program which prints number of occurrences of each characters and also it should not print repeatedly occurrences of duplicate characters as given in the example:
Examples: 

Input : neveropen
Output :
Number of Occurrence of g is:2
Number of Occurrence of e is:4
Number of Occurrence of k is:2
Number of Occurrence of s is:2
Number of Occurrence of f is:1
Number of Occurrence of o is:1
Number of Occurrence of r is:1

Approach:

The idea is to create a count array of size 256. Traverse input string and for every character increment its count. 

JAVA




// Java program for the above approach
class NoOfOccurrenceOfCharacters {
    static final int MAX_CHAR = 256;
 
    static void getOccurringChar(String str)
    {
         
        // Create an array of size 256
        // i.e. ASCII_SIZE
        int count[] = new int[MAX_CHAR];
 
        int len = str.length();
 
        // Initialize count array index
        for (int i = 0; i < len; i++)
            count[str.charAt(i)]++;
 
        // Create an array of given String size
        char ch[] = new char[str.length()];
        for (int i = 0; i < len; i++) {
            ch[i] = str.charAt(i);
            int find = 0;
            for (int j = 0; j <= i; j++) {
 
                // If any matches found
                if (str.charAt(i) == ch[j])
                    find++;
            }
 
            if (find == 1)
                System.out.println(
                    "Number of Occurrence of "
                    + str.charAt(i)
                    + " is:" + count[str.charAt(i)]);
        }
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        String str = "neveropen";
        getOccurringChar(str);
    }
}


Output

Number of Occurrence of g is:2
Number of Occurrence of e is:4
Number of Occurrence of k is:2
Number of Occurrence of s is:2
Number of Occurrence of f is:1
Number of Occurrence of o is:1
Number of Occurrence of r is:1

Time complexity: O(n2)

Auxiliary Space: O(256)

Example: Count total number of character in the string.

Java




/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.Scanner;
class Program {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        String str="GeeksForGeeks"
        System.out.println(
            "Total number of character in string is "
            +str.length());
    }
}


Output

Total number of character in string is 13

Counting occurrence of letter in string using HashMap

In this approach we will use HashMap as well as ArrayList to count exact occurrence of letter in given String.

Java




import java.io.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        String str = "GeeksForGeeks";
        ArrayList<Character> al = new ArrayList<>();
        for (int i = 0; i < str.length(); i++) {
            al.add(str.charAt(i));
        }
        HashMap<Character, Integer> hm = new HashMap<>();
        for (int i = 0; i < str.length(); i++) {
            hm.putIfAbsent(al.get(i),
                           Collections.frequency(al,al.get(i)));
        }
 
        for (Map.Entry<Character,Integer> e : hm.entrySet()) {
            System.out.println(
                "Character is :"+e.getKey()+ " and its count is string is "+ e.getValue());
        }
    }
}


Output

Character is :r and its count is string is 1
Character is :s and its count is string is 2
Character is :e and its count is string is 4
Character is :F and its count is string is 1
Character is :G and its count is string is 2
Character is :k and its count is string is 2
Character is :o and its count is string is 1
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments