-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharOccurance_Assignment3.java
More file actions
42 lines (25 loc) · 954 Bytes
/
CharOccurance_Assignment3.java
File metadata and controls
42 lines (25 loc) · 954 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
39
40
41
42
package week1.day2;
public class CharOccurance_Assignment3 {
public static void main(String[] args) {
String str = "welcome to chennai";
char search = 'e';
int count =0;
char [] MyArray = str.toCharArray();
int l= MyArray.length;
for (int i=0; i<l ;i++) {
if (MyArray[i]== search) {
count++;
}
System.out.println("The Character '"+search+"' appears "+count+" times.");
}
}
}
//Check number of occurrences of a char (eg 'e') in a String
//String str = "welcome to chennai";
// declare and initialize a variable count to store the number of occurrences
// convert the string into char array
//get the length of the array
// traverse from 0 till the array length
// Check the char array has the particular char in it
// if is has increment the count
// print the count out of the loop