Chrome File Edit View History Bookmarks People Window Help 8
Solution
Occurances.java
public class Occurances {
public static void main(String[] args) {
java.util.Scanner scan = new java.util.Scanner(System.in);
System.out.println(\"Enter the string: \");
String s = scan.nextLine();
System.out.println(\"Enter the character: \");
char ch = scan.next().charAt(0);
int n = count(s,ch);
System.out.println(\"Number of occurances of a character \"+ch+\" is : \"+n);
}
public static int count(String str,char a){
int count = 0;
for(int i=0; i<str.length();i++){
if(str.charAt(i) == a){
count++;
}
}
return count;
}
}
Output:
Enter the string:
welcome
Enter the character:
e
Number of occurances of a character e is : 2
