-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstLastOccurence.java
More file actions
33 lines (30 loc) · 990 Bytes
/
FirstLastOccurence.java
File metadata and controls
33 lines (30 loc) · 990 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
package prachi.stringpractice;
public class FirstLastOccurence{
public static void main(String[] args) {
String str = new String("Hello, World");
char ch = 'o';
System.out.println("The first occurence of " + ch + " in " + str + " is " + First(str, ch));
System.out.println("The last occurence of " + ch + " in " + str + " is " + Last(str, ch));
}
public static int First(String s, char c) {
int index1 = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
index1 = i;
break;
}
}
return index1;
}
//method for finding first occurence from last
public static int Last(String s, char c) {
int index2 = 0;
for (int i = (s.length()-1); i >= 0; i--) {
if (s.charAt(i) == c) {
index2 = i;
break;
}
}
return index2;
}
}