-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfindcharinstring.java
More file actions
44 lines (38 loc) · 1.15 KB
/
findcharinstring.java
File metadata and controls
44 lines (38 loc) · 1.15 KB
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
39
40
41
42
43
44
/*Java program to find the occurrence of a character in a string */
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);
}
}