Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text
In this program, we try to learn and find the frequency count of vowels, consonants, special symbols, digits, words in a given string.
Description: frequency count of Vowels, Consonants, Symbol, Digit & Words
The input for this program is a line of text and the output of how many vowels, consonants, digits, and special characters that given lines contain. Firstly, read a line of text from the user and store that line as a string of name ‘n’. Now read each and every character of the string n using the method charAt() pre-defined in String class. Check each and every character, ch, whether it is a vowel or consonant. If the ch is one from ‘a, e, i, o, u, A, E, I, O, U’ then it is a vowel otherwise it is a consonant. If ch is not a vowel or consonant, it may be a digit or special character, or space. If ch is any number between 0-9, then it is a digit. Ch may be a space. If ch is not any one of these cases, then definitely it is a special character or special symbol.
Algorithm: frequency count of Vowels, Consonants, symbols, digits & Words
- import java.io.*; and import java.util.*;
- Create class Count{
- Create main function: public static void main(String ar[])
- Input as Integer: int v = 0, c = 0, d = 0, sm = 0, sp = 0, w = 0;
- Print user message to enter text.
- Scanner s = new Scanner(System. in);
- Read String n = s.nextLine();
- for (int i=0;i < n.length();i++){
- char ch=n.charAt(i);
- Compare each digit and then increment digit.
- if (ch == ‘0’ | | ch == ‘1’ | | ch == ‘2’ | | ch == ‘3’ | | ch == ‘4’ | | ch == ‘5’ | | ch == ‘6’ | | ch == ‘7’ | | ch == ‘8’ | | ch == ‘9’)
d++;
- if (ch == ‘0’ | | ch == ‘1’ | | ch == ‘2’ | | ch == ‘3’ | | ch == ‘4’ | | ch == ‘5’ | | ch == ‘6’ | | ch == ‘7’ | | ch == ‘8’ | | ch == ‘9’)
- Now compare vowel letter.
- else if (ch == ‘a’ | | ch == ‘e’ | | ch == ‘i’ | | ch == ‘o’ | | ch == ‘u’ | | ch == ‘A’ | | ch == ‘E’ | | ch == ‘I’ | | ch == ‘O’ | | ch == ‘U’)
v++; - If not match then compare for constant
- else if (ch >= 65 & & ch <= 123)
c++;
- else if (ch >= 65 & & ch <= 123)
- else if (ch == ‘a’ | | ch == ‘e’ | | ch == ‘i’ | | ch == ‘o’ | | ch == ‘u’ | | ch == ‘A’ | | ch == ‘E’ | | ch == ‘I’ | | ch == ‘O’ | | ch == ‘U’)
- Compare with space: else if (ch == ‘ ‘)
{
sp++;
w++;
} - else print special symbol: else sm++;
- Now Print all count:
- System.out.println(“Vowels : “+v);
System.out.println(“Consonants : “+c);
System.out.println(“Digits : “+d);
System.out.println(“Spaces : “+sp);
System.out.println(“Symbols : “+sm);
System.out.println(“Words : “+w);
- System.out.println(“Vowels : “+v);
Source Code: frequency count of Vowels, Consonants, symbols, digits & Words
import java.io. *; import java.util. *; public class Count { public static void main(String ar[]) { int v = 0, c = 0, d = 0, sm = 0, sp = 0, w = 0; System.out.println("Enter text : "); Scanner s = new Scanner(System. in); String n = s.nextLine(); for (int i=0;i < n.length();i++) { char ch=n.charAt(i); if (ch == '0' | | ch == '1' | | ch == '2' | | ch == '3' | | ch == '4' | | ch == '5' | | ch == '6' | | ch == '7' | | ch == '8' | | ch == '9') d++; else if (ch == 'a' | | ch == 'e' | | ch == 'i' | | ch == 'o' | | ch == 'u' | | ch == 'A' | | ch == 'E' | | ch == 'I' | | ch == 'O' | | ch == 'U') v++; else if (ch >= 65 & & ch <= 123) c++; else if (ch == ' ') { sp++; w++; } else sm++; } System.out.println("Vowels : "+v); System.out.println("Consonants : "+c); System.out.println("Digits : "+d); System.out.println("Spaces : "+sp); System.out.println("Symbols : "+sm); System.out.println("Words : "+w); } }
Sample output :
Enter Text : HI! we are from MITS established in 1998. Vowels:11 Consonants:17 Digits:4 Spaces:7 Symbols:2 Words :7