Java program to count the occurrence of any character in given String.
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
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: Core Java, Interview Question, Java, Programs, String, Written Test
Learn Hadoop Online Training with a Free Live Demo.Complete Interactive Classes includes Core Java Topics.
ReplyDeletehttp://www.qedgetech.com/big-data-hadoop-online-training