Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text

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

  1. import java.io.*; and import java.util.*;
  2. Create class Count{
  3. Create main function: public static void main(String ar[])
  4. Input as Integer: int v = 0, c = 0, d = 0, sm = 0, sp = 0, w = 0;
  5. Print user message to enter text.
  6. Scanner s = new Scanner(System. in);
  7. Read String n = s.nextLine();
  8. for (int i=0;i < n.length();i++){
  9. char ch=n.charAt(i);
  10. Compare each digit and then increment digit.
    1. if (ch == ‘0’ | | ch == ‘1’ | | ch == ‘2’ | | ch == ‘3’ | | ch == ‘4’ | | ch == ‘5’ | | ch == ‘6’ | | ch == ‘7’ | | ch == ‘8’ | | ch == ‘9’)
      d++;
  11. Now compare vowel letter.
    1. else if (ch == ‘a’ | | ch == ‘e’ | | ch == ‘i’ | | ch == ‘o’ | | ch == ‘u’ | | ch == ‘A’ | | ch == ‘E’ | | ch == ‘I’ | | ch == ‘O’ | | ch == ‘U’)
      v++;
    2. If not match then compare for constant
      1. else if (ch >= 65 & & ch <= 123)
        c++;
  12. Compare with space: else if (ch == ‘ ‘)
    {
    sp++;
    w++;
    }
  13. else print special symbol: else sm++;
  14. Now Print all count:
    1. 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);

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

Learn More:

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment