-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringOccr.java
38 lines (28 loc) · 856 Bytes
/
StringOccr.java
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
/*Find the locations and count of occurrences of a character in a
string */
import java.util.Scanner;
class StringOccr
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string : ");
String str = sc.nextLine();
System.out.print("Enter a character : ");
char ch = sc.next().charAt(0);
int count = 0;
System.out.println("Locations of occurrences : ");
for(int i =0; i<str.length();i++)
{
if(str.charAt(i) == ch)
{
System.out.println("Index " + i);
count++;
}
}
if (count > 0)
System.out.println("The character '" + ch + "' occurs " + count + " time(s) in the string.");
else
System.out.println("The character '" + ch + "' does not occur in the string.");
}
}