Written by Sai Sameera Thota on March 18, 2014 in Java > Programming

Java program to find the frequency of a given letter in Text

GET ALERTS:

Get our Latest updates delivered to your mailbox!

In response to one of our readers, we have written this Java Program to find the Frequency of a given Letter in Text. You can read Java Program to Count Frequency of Words in given Text here.

In this program we are taking a String input for Text and another String input for Letter to be searched. After that we are using a for loop to search for the frequency of a given Letter in the Text string. If the Letter is found, then we are incrementing the count variable else we are continuing the iteration. After the iteration is complete we are printing the Frequency of given letter using the count variable.

Java program to find the frequency of a given letter in Text

import java.io.*;
class Letters 
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int count = 0;
		System.out.print("\nEnter some text : ");
		
String str = br.readLine();
		System.out.print("\nEnter the letter to be searched : ");
		String ltr = br.readLine();
		for(int i=0;i<str.length();i++)
		{
			if(str.charAt(i)==ltr.charAt(0))
				count++;
			else
				continue;
		}
		System.out.print("\nFrequency of the letter : "+count);

	}
}

Output:
java_letter_frequency

Download Source Code

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: