DevJourney · code I wrote while learning

Number of Occurrences of a Specific Character in a Given String

2 items

README.md

Number of Occurrences of a Specific Character in a Given String

Single File Programming Question

Problem Statement

Sophie has been tasked with creating a program using the java.util.Scanner package that counts the number of occurrences of a specific character in a given string. The program should prompt the user to enter a string and then a single character. It should then compute how many times that character appears in the string and display the result.

Your task is to help Sophie in implementing the same.

Input:

format :

The first line of input contains a string S.

The second line contains a character C.

Output:

format :

The output prints a single line indicating the number of times the character C appears in the string S in the format: "The character 'C' occurs X times."

Refer to the sample output for formatting specifications.

Code constraints : The given test cases fall under the following constraints:

1 ≤ length of string ≤ 100

The string S will contain only printable ASCII characters, including spaces.

characters are case-sensitive.

Sample test cases :

Input 1:

apple
p
Output 1 :
The character 'p' occurs 2 times.
Input 2 :
OpenAI is creating artificial intelligence
a
Output 2 :
The character 'a' occurs 3 times.
Input 3 :
Apple
P
Output 3 :
The character 'P' occurs 0 times.