Wednesday, 11 January 2017

Program to check the occurrence of character in a string

import java.util.Scanner;
public class Occurancecharacter {
public static void main(String[] args) {
int i=0;
String s=”raakesh”;
char[] c = s.toCharArray();
System.out.println(“Enter the character”);
Scanner sc = new Scanner(System.in);
char a=sc.next().charAt(0);
for(int k=0;k<c.length;k++)
{
if(c[k]==a)
{
i++;
}
}
System.out.println(i);
}
}
Output :-
Enter the character :
a
2

No comments:

Post a Comment