Answer:
//Java program to find the number of occurrences of a specified character in a string.
// Import the required packages.
import java.io.*;
import java.util.*;
//class declaration
class CharacterCount
{
// Main method.
public static void main (string args[ ] )
{
try
{
// Taking the input from the user.
System. out.print ("Type a string to analyze: ");
// Creating the scanner class object.
scanner input = new scanner (system. in);
String str = input .next();
System.out.print("Type a character to check: ")
String temp = input.next();
char c = temp.charAt(0);
// Calling the method to get the character count.
int count = <em>count</em>(str,c);
// Displaying the result.
system.out.print In("There are " + count + " " + c + " 's. ");
// catching the exception.
catch(Exception e)
System.out.print In("Exception has occured in the class . Program will exit. ");
// Exiting the system.
System.exit(0);
}
}
// Method to calculate the character count.
public static int count(String str, char a)
{
// checking for null string input.
if(str.equals(""))
{
return 0;
}
else
{
int k = 0;
// Character matching check.
if ( str.substring(0, 1) . equals (Character.<em>toString</em>(a)) )
{
// Incrementing the counter.
k++;
}
// Recursive call
k += <em>count</em>(str.substring(1) , a);
return k;
}
}
}
Explanation:
Output should show: Type a string to analyze: welcome
Type a character to check: e
There are 2 e's.