How to count the occurrence of the given character in a string ?

How to count the occurrence of the given character in a string ?

Program
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        int x,n,count=0;
        String s;
        char ch;
        		
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter String :");
        s=sc.nextLine();
        System.out.println("Enter Character: ");
        ch=sc.next().charAt(0);
        n=s.length();
        for(int i=0; i<s.length(); i++)
        {
            // checking character in string
            if (s.charAt(i) == ch)
            {
                count++;
            }
        } 
        System.out.println("Number of occurrences are : "+count);
    }

}

Output:

Enter String : sateesh
Enter Character: e
Number of occurrences are : 2





More Questions


16 . How to reverse a string in Java without using the reverse method ?
17 . How to count the occurrence of the given character in a string?
18 . How to check if a string is a palindrome?
19 . How to count the number of words in a given string sentence?
20 . How to reverse the words from the given string sentence?
21 . How to swap two strings without using the third variable?