forked from bediacademy/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoccurence_of_char.java
More file actions
38 lines (37 loc) · 979 Bytes
/
occurence_of_char.java
File metadata and controls
38 lines (37 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class CountOccuranceOfChar1
{
static final int MAX_CHAR = 256;
static void getOccuringChar(String str)
{
//creating an array of size 256 (ASCII_SIZE)
int count[] = new int[MAX_CHAR];
//finds the length of the string
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)
//prints occurrence of the character
System.out.println("The occurrence of "+ str.charAt(i)+ " is: " + count[str.charAt(i)]);
}
}
//driver Code
public static void main(String args[])
{
String str = "Pneumonoultramicroscopicsilicovolcanoconiosis"; //lung disease
//function calling
getOccuringChar(str);
}
}