Occurrence of a character

Given a sentence S and a character ch, count occurrence of the given character in the sentence

Input Format

First line of input contains a sentence – S and second line of input contains a single lowercase character – ch.

Constraints

1 <= len(S) <= 100

Output Format

Print count of the given character in the sentence.

InputOutput
Data Structures & Algorithms
s
2
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String S = sc.nextLine().trim();
        char ch = sc.nextLine().trim().charAt(0);
        
        int count = Target(S, ch);
        System.out.println(count);
    }
    private static int Target(String S, char c) {
        int count = 0;
        for (int i = 0; i < S.length(); i++) {
            if (S.charAt(i) == c) {
                count++;
            }
        }
        return count;
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top