String programs part 4
Write a program to input a sentence and print the frequency of the double occurrence characters.
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
Related Posts
Continue learning with these related articles

String Methods in Java for ICSE Class 10: 25 Output Questions with Answers
ICSE Class 10 Computer Applications practice on String class methods: length, charAt, indexOf, lastIndexOf, substring, replace, trim, compareTo, equals and more, with 25 board-style output questions and step-by-step answers.

Special Number Programs in Java for ICSE: Armstrong, Spy, Neon, Disarium, Pronic and More
Every special number asked in ICSE Computer Applications – prime, perfect, Armstrong, palindrome, automorphic, neon, spy, Niven, duck, buzz, Disarium, special and pronic numbers – with definitions, examples and one complete Java program that checks them all.

Math Library Methods in Java for ICSE Class 9 (with Outputs)
ICSE Class 9 Computer Applications notes on the Java Math class: sqrt, cbrt, pow, abs, max, min, ceil, floor, round, rint and random, with return types, tricky negative-number outputs and Java expressions for formulas.