forked from NischayKG/My_Java_Programs_of_Strings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_and_last_occurance.java
More file actions
37 lines (34 loc) · 1004 Bytes
/
first_and_last_occurance.java
File metadata and controls
37 lines (34 loc) · 1004 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.Program to find First and Last Occurrence of given Character in a String
import java.util.*;
class first_and_last_character
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.println("ENter the string :");
String str=new String(sc.nextLine());
System.out.println("ENter the character :");
char ch=sc.next().charAt(0);
int s=0,e=0;
for(int i=0;i<str.length();i++)
{
if(s==0&&e==0)
{
if(str.charAt(i)==ch)
{
s=i;
s++;
}
}
else
{
if(str.charAt(i)==ch)
{
e=i;
e++;
}
}
}
System.out.println("The First and last occurance of character "+ch+" is "+s+" and "+e+" resprctively");
}
}