Alphabet Frequency Program in Java and Python
Alphabet frequency counting program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
Write a program to input a string. Convert it into lowercase letters. Count and print the frequency of each alphabet present in the string.
Algorithm:
Step 1: Start.
Step 2: Accept a string from the user.
Step 3: Convert the string to lowercase.
Step 4: Store the length of the string.
Step 5: Run an outer loop from character 'a' to 'z'.
Step 6: For each alphabet, initialize count = 0.
Step 7: Run an inner loop through every character of the string.
Step 8: If the current string character matches the alphabet from the outer loop, increase count.
Step 9: After scanning the string, print the alphabet only if count is not zero.
Step 10: Continue until all alphabets have been checked.
Step 11: Stop.
Explanation:
The program counts alphabet frequencies by checking one alphabet at a time. The input string is first converted to lowercase. This makes the counting case-insensitive, so uppercase and lowercase forms of the same letter are counted together.
The outer loop runs from 'a' to 'z'. In each pass, the loop variable represents the alphabet whose frequency is currently being counted. A fresh count variable is set to 0 before scanning the string, because each alphabet must have its own separate count.
The inner loop scans the complete input string from the first character to the last character. For every position, the character at that position is compared with the current alphabet from the outer loop. If both are equal, count is increased by 1. After the inner loop finishes, the program knows the frequency of that one alphabet. The program prints the alphabet only if count is not zero. This keeps the output clean because letters that are not present in the string are not displayed.
This nested-loop approach is simple but very clear for ICSE and ISC level understanding. The outer loop chooses the alphabet to be counted, and the inner loop checks the whole string for that alphabet. Because the counter is reset inside the outer loop, each alphabet gets an independent frequency. The program also avoids using advanced library methods, making the counting process visible. This is useful for students because they can see exactly how character comparison and counters work together to produce a frequency table.
Java Program:
/**
* The class AlphabetFreq inputs a string and counts the frequency of each alphabet present in it
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class AlphabetFreq
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter any string: ");
String s = sc.nextLine().toLowerCase(); // Convert once so A and a are counted together.
int l = s.length();
System.out.println("Output:");
System.out.println("==========================");
System.out.println("Alphabet\tFrequency");
System.out.println("==========================");
for(char i = 'a'; i <= 'z'; i++)
{
int count = 0; // Fresh counter for the current alphabet.
// Scan the complete string for the current alphabet.
for(int j = 0; j < l; j++)
{
char ch = s.charAt(j);
if(ch == i)
count++;
}
if(count != 0)
System.out.println(i + "\t\t" + count);
}
}
}Equivalent Python Program:
s = input("Enter a string: ").lower()
# Check every alphabet from a to z separately.
for code in range(ord('a'), ord('z') + 1):
ch = chr(code)
count = 0
# A fresh count is maintained for the current alphabet only.
# Count how many times the current alphabet occurs in the string.
for i in range(len(s)):
if s[i] == ch:
count = count + 1
if count > 0:
print(ch, "=", count)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.