In: Computer Science
Write a complete Java program called CharCounter that gets two Strings called inputWord and inputCharacter from the user at the command line. Check that the character has a length of 1.
If it doesn't, provide the user with suitable feedback and conclude the program.
If the character length is valid (i.e., it has a length of 1), use a while loop to check each position in the inputWord variable and return the number of times the character occurs. For example, if the inputWord is "test" and the inputCharacter is "e" you would output: "There is 1 occurrence of 'e' in test."
CharacterCounter.java
import java.util.Scanner;
public class CharacterCounter {
   public static void main(String[] args) {
       //Declaruing variables
       String
inputWord,inputCharacter;
       int count=0,i=0;
      
       //Scanner object is used to get the
inputs entered by the user
       Scanner sc=new
Scanner(System.in);
      
       //Getting the word entered by the
user
       System.out.print("Enter the Input
Word :");
       inputWord=sc.next();
      
       //Getting the character entered by
the user
       System.out.print("Enter the
charcater :");
       inputCharacter=sc.next();
      
       //if the length of the character is
greater than 1 the display error message
      
if(inputCharacter.length()>1)
       {
          
System.out.println("** Invalid.Length of the character should not
be more than 1 **");
       }
       else
       {
           i=0;
           //This while
loop will count the number of occurrences of that character
      
while(i<inputWord.length())
       {
          
if(inputCharacter.charAt(0)==inputWord.charAt(i))
           {
          
    count++;
          
      
           }
           i++;
       }
       }
       //Displaying the result
       System.out.println("There are
"+count+" occurance of '"+inputCharacter+"' in "+inputWord);
}
}
_______________
Output:
Enter the Input Word :test
Enter the charcater :e
There are 1 occurance of 'e' in test
______________Thank You