Computer Science, asked by MansiKatoch, 1 day ago

Write a program to enter a string and an alphabet. Now count how many times the given alphabet is present in the string and display the output. Sample input: welcome to Java alphabet: e Output: frequency of alphabet e is: 2​

Answers

Answered by samarthkrv
1

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) throws Exception {

    Scanner sc = new Scanner(System.in);

 System.out.print("Enter a String:");

 String x = sc.nextLine();

 System.out.print("Now enter a char:");

 char c = (char)System.in.read();

 int count = 0;

     for(int i = 0; i < x.length(); i++){

         if(Character.toUpperCase(x.charAt(i)) == Character.toUpperCase(c)){

             count++;

         }

     }

 System.out.println("The number of times " + c + " occurs in the string " + x + " is " + count);

}

}

Explanation:

Similar questions