Count occurrences of Character in String in Java

In this program we will count occurrences of character in string . We will give input a character and string in this program. We have different method to count the number of occurrences of a character in a string in java .
  1. Count occurrences of character in string java using hashmap
  2. Count number of characters in a string Using String Library Methods
  3. Count occurrences of character in string using Java8 stream
  4. Count occurrences of character in string java using Recursion

Count occurrences of character in string java using hashmap

In this program we will use hashmap to find frequency of characters in a string in java . We will create Map<Character, Integer> and iterate string . Now check char is present in map then increase it count by 1 otherwise add char with count 1 in map .

import java.util.HashMap;
import java.util.Map;

public class CountCharUsingMap {
	
	public static void main(String[] args) {
		String str  = "javavogueprogram";
		char ch = 'a';
		
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		for (int i =0 ; i < str.length() ; i++) {
			if (map.containsKey(str.charAt(i))) {
				
				map.put(str.charAt(i), map.get(str.charAt(i))+1);
				
			} else {
				map.put(str.charAt(i),1);
			}
			
		}
		
		int count = map.get(ch);
        System.out.println("all occurrences of character : "+ ch +" , 
              in string : "+str+" , is : "+ count);
	}
}
Output:
all occurrences of character : a , in string : javavogueprogram , is : 3

Count number of characters in a string Using String Library Methods

We can find all occurrences of character in string java using charAt() method . We iterate string and each iteration we check if char of string with the help of charAt(index) is equal to given char , then we will increase count .

public class CountCharOccurancejava {

	public static void main(String[] args) {
		String str = "javavogueprogram";
		char ch = 'a';
		int count = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == ch) {
				count++;
			}
		}
		System.out.println("all occurrences of character : " + ch
				+ " , in string : " + str + " , is : " + count);
	}
}
Output:
all occurrences of character : a , in string : javavogueprogram , is : 3

Count occurrences of character in string using Java8 stream

In this example we will count occurrences of character in string java using  java8 stream. We willuse chars() method and filter for count number of characters in a string.
public class CountCharOccurancejava {

	public static void main(String[] args) {
		String str = "javavogueprogram";
		char ch = 'a';
		long count = str.chars().filter(c -> c == ch).count();
		System.out.println("all occurrences of character : " + ch
				+ " , in string : " + str + " , is : " + count);
	}
}
Output :
all occurrences of character : a , in string : javavogueprogram , is : 3

Count occurrences of character in string java using Recursion

In this example we will write a program to find frequency of characters in a string in java using recursion . We will start iterate string from 0 and iterate string till end . Each iteration we increase count by 1 if  match with given char .
public class CountChaUsingRecursion {
	
	public static void main(String[] args) {
		String str  = "javavogueprogram";
		char ch = 'a';
		int count = findCharOccurrences(str, ch, 0);
		System.out.println("all occurrences of character : "+ ch +" 
                          , in string : "+str+" , is : "+ count);
	}
	
	 static int findCharOccurrences(String str, char ch , int index)
	  {
	      if(index >= str.length())
	      return 0;
	      
	      int count=0;
	      
	      if(str.charAt(index) == ch)
	      count++;
	      
	      return count + findCharOccurrences(str,ch,index+1);
	  }

}
Output:
all occurrences of character : a , in string : javavogueprogram , is : 3
In this article we have seen different ways to count occurrences of character in string in java. Now you can count char in string java using different methods. You can learn more interesting java coding program  for interview .

Leave a Reply

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

35 + = 37