You are on page 1of 3

GEEKSFORGEEKS

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 : geeksforgeeks
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
Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

class NoOfOccurenceOfCharacters {
static final int MAX_CHAR = 256;

static void getOccuringChar(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)]);
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = "geeksforgeeks";
getOccuringChar(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

Bishal Kumar Dubey


Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator. I just love
Programming languages and love to know new concepts everyday,every minute,every second.
Here to help Other GEEKS!!!
If you like GeeksforGeeks and would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your
article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above.
Practice Tags : Java
Categories: Java, Java Programs
Related Posts
Program to convert first character uppercase in a sentence
Output of Java Programs | Set 54 (Vectors)
Comparator Interface in Java with Examples
Importance of Hashcode method in Java
SerialVersionUID in Java
JavaFX | Button with examples
Android | Starting with first app/android project
Life Cycle of a Servlet
BigDecimal doubleValue() Method in Java
BigDecimal unscaledValue() in Java

Read Full Article


710-B, Advant Navis Business Park,
Sector-142, Noida, Uttar Pradesh - 201305
feedback@geeksforgeeks.org
COMPANY
About Us
Careers
Privacy Policy
Contact Us
LEARN
Algorithms
Data Structures
Languages
CS Subjects
Video Tutorials
PRACTICE
Company-wise
Topic-wise
Contests
Subjective Questions
CONTRIBUTE
Write an Article
GBlog
Videos

@geeksforgeeks, Some rights reserved

You might also like