Answer:
import java.util.Scanner;
public class num4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a word");
String word = in.nextLine();
System.out.println("enter a character");
char c = in.nextLine().charAt(0);
int count=0;
for(int i =0; i < word.length(); i++){
if(word.charAt(i)==c){
count++;
}
}
System.out.println(count);
}
}
Step-by-step explanation:
- Implemented in Java
- Prompt and receive user input for a string and a character
- Use a for loop to iterate through each character of the string
- Within the for loop check for the occurrence of the character and increment the variable count already initialized Using Java Programming Language
- Outside the for loop print count