Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.

Java program to count the occurrence of any character in given String.

Somdeb Burman | 11:13:00 AM | 1 comments

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

class CountFrequencyOfWord {
 public static void main(String args[]) {
  try {
   input();
  } catch (IOException ex) {
   Logger.getLogger(CountFrequencyOfWord .class.getName()).log(Level.SEVERE, null, ex);
  }
 }
 static void input() throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter a sentence");
  String s = br.readLine();
  System.out.println("Enter the letter ");
  char a = (char) br.read();
  int len = s.length();
  int c = 0;
  char x;
  for (int i = 0; i < len; i++) {
   x = Character.toLowerCase(s.charAt(i));
   if (x == a)
    c = c + 1;
  }
  System.out.println("Frequency of the letter  " + a + " in the sentence " + s + " is " + c);
 }
}



Output:

Enter a sentence
Hi my name is umesh
Enter the letter 
m
Frequency of the letter  m in the sentence "Hi my name is umesh" is 3

Java Resources are available for Download from below

Category: , , , , ,

1 comment:

  1. Learn Hadoop Online Training with a Free Live Demo.Complete Interactive Classes includes Core Java Topics.
    http://www.qedgetech.com/big-data-hadoop-online-training

    ReplyDelete