Ex:- str = "codeheist" c = 'e' occurence = 2
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Scanner; public class CountOccurence { public static void main(String[] args) { int count = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter the String"); String str = sc.nextLine(); System.out.println("Enter the Character"); char c = sc.next().charAt(0); for (int i = 0; i < str.length(); i++) { if (c==str.charAt(i)) count++; } System.out.println(c+ " Occurred "+ count + " time."); } } |
Python
input_string = input("Enter a string:") ch = input("Enter a character:") ch_count = input_string.count(ch) print("count of",ch,"is",ch_count) |