Java program to find the count / frequency of input character in the string


Program
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int count=0; System.out.println("Enter the string"); String str=sc.nextLine(); System.out.println("Enter the character"); char ch=sc.next().charAt(0); for(int i = 0; i < str.length(); i++) { if(ch == str.charAt(i)) { count = count+1; } } System.out.println("Total count of character '"+ch+"' is "+count); } }
Input
Output