Skip to content

Instantly share code, notes, and snippets.

@aadipoddar
Created April 14, 2022 14:19
Show Gist options
  • Save aadipoddar/eab4907345b0544090502b6f95652c33 to your computer and use it in GitHub Desktop.
Save aadipoddar/eab4907345b0544090502b6f95652c33 to your computer and use it in GitHub Desktop.
Find Frequency of each alphabet present
/*
Accept a senetnce and print the frequency of each alphabet present
INPUT: Smooth Road
OUTPUT:
s: 1
m: 1
o: 3
t: 1
h: 1
r: 1
a: 1
d: 1
*/
import java.util.Scanner;
class AlphabetFrequency {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence");
String sen = sc.nextLine();
String sentence = "";
for (int i = 0; i < sen.length(); i++)
if (sen.charAt(i) != ' ')
sentence += sen.charAt(i);
sentence = sentence.toLowerCase();
int alphabets[] = new int[26];
for (int i = 0; i < sentence.length(); i++) {
int index = sentence.charAt(i) - 'a';
alphabets[index]++;
}
for (int i = 0; i < 26; i++) {
if (alphabets[i] != 0) {
System.out.println((char) (i + 'a') + ": " + alphabets[i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment