How to find occurrence of a character in a string java ?

  Java Interview Q&A

Dear readers, today we create a Java program, which is that we are detecting the occurrence of every character in a string. To do this we are first creating an array of size 256 (ASCII upper range), the idea here is to store a count that goes against the ASCII value of that character. For example, the occurrence of ‘A’ will be stored in the counter [65] because the ASCII value of A is 65, similarly events from other charts are stored against their ASCII index values.

Then we are creating another array array to hold the characters of the given string, then we are comparing them with the characters in the string and using a counter array when a match is found that count of that particular character Is displayed.

Java Program to find occurrence of a character in a string

public class JavaExample {  

   static void countEachChar(String str) 
   { 
	//ASCII values ranges upto 256
	int counter[] = new int[256]; 

	//String length
	int len = str.length(); 

	/* This array holds the occurrence of each char, For example
	 * ASCII value of A is 65 so if A is found twice then 
	 * counter[65] would have the value 2, here 65 is the ASCII value
	 * of A
	 */
	for (int i = 0; i < len; i++) 
		counter[str.charAt(i)]++; 

	// We are creating another array with the size of String
	char array[] = new char[str.length()]; 
        for (int i = 0; i < len; i++) { 
	   array[i] = str.charAt(i); 
	   int flag = 0; 
	   for (int j = 0; j <= i; j++) { 

		/* If a char is found in String then set the flag 
		 * so that we can print the occurrence
		 */
		if (str.charAt(i) == array[j])  
			flag++;                 
	   } 

	   if (flag == 1)  
	      System.out.println("Occurrence of char " + str.charAt(i)
		 + " in the String is:" + counter[str.charAt(i)]);             
	} 
   } 
   public static void main(String[] args) 
   {  
	String str = "fastreadjiqa"; 
	countEachChar(str); 
   } 
}

Output: 

Occurrence of Char a in the String is : 3
Occurrence of Char e in the String is : 1
Occurrence of Char t in the String is : 1

Java program to check occurrence of each character in String

We can use Java’s map utility to detect the occurrence of each character in a string. A map key cannot be duplicated, so make each character of the string as the key of the map and provide the initial value corresponding to each key if it is the character not previously inserted in the map. Now when a character repeats during insertion as a key in the map, increase its value one by one. Close it for each character Insert all the characters in the string.

public class occurenceOfCharacter {
   public static void main(String[] args) {
      String str = "SSDRRRTTYYTYTR";
      HashMap <Character, Integer> hMap = new HashMap<>();
      for (int i = str.length() - 1; i > = 0; i--) {
         if (hMap.containsKey(str.charAt(i))) {
            int count = hMap.get(str.charAt(i));
            hMap.put(str.charAt(i), ++count);
         } else {
            hMap.put(str.charAt(i),1);
         }
      }
      System.out.println(hMap);
   }
}

Output

{D=1, T=4, S=2, R=4, Y=3}

Find Frequency of Character Java

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 = 4

Read Here Complete Java Tutorial – Click Here

LEAVE A COMMENT