BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java program to find the occurrence of a character in a string

Last Updated: April 10, 2019 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a Java program to find the occurrence of a character in a String.

Program to find occurrence of a character in a string

In this program we are finding the occurrence of each 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 the occurrence count against the ASCII value of that character. For example, the occurrence of ‘A’ would be stored in counter[65] because ASCII value of A is 65, similarly occurrences of other chars are stored in against their ASCII index values.

We are then creating an another array array to hold the characters of the given String, then we are comparing them with the characters in the String and when a match is found the count of that particular char is displayed using counter array.

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 = "beginnersbook"; 
	countEachChar(str); 
   } 
}

Output:
Java program to find the occurrence of a character in a string

Related Java programs

1. Java program to sort strings in an alphabetical order
2. Java program to check vowel and consonant
3. Java program to find duplicate characters in a String
4. Java program to check palindrome String

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. java program to find factorial of a given number using recursion
  3. Java program to print number of elements in an array
  4. Tech Number Program in Java
  5. Java Program to find longest substring without repeating characters

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap