Write a program to input a sentence and print the frequency of the double occurrence characters.
import java.io.*;
class DoubleOccur
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence");
String str= br.readLine();
int len=str.length();
int count=0;
for (int i=0;i<len-1;i++)
{
char ch=str.charAt(i);
char ch1=str.charAt(i+1);
if(ch==ch1)
count++;
}
System.out.println("The frequency of the double occurrence is"+count);
}
}
Output:-
I am good
The frequency of the double occurrence is: 1
Write a program to input a sentence and a word and print the frequency of the word in a sentence.
import java.io.*;
class WordFrequency
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence");
String str= br.readLine();
System.out.println("Enter a word to check for the frequncy");
String wrd= br.readLine();
int len=str.length();
int d=0; int count=0;
for (int i=0;i<len;i++)
{
char ch=str.charAt(i);
if(Character.isWhitespace(ch))
{
String temp=str.substring(d,i);
d=i;
if(temp.equals(wrd)
count++;
System.out.println(“The frequency of the word is”+count);
}
}
}
}
Output:-
Enter a sentence
This is a school
Enter a word
school
The frequency of the word is 1