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:

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:

  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:

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
Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text wikipedia

FAQ:

How do you count a number of vowels and consonants in a given string in Java?

To count a number of vowels and consonants first, we have to take user input
n = s.nextLine(); Then we have to compare character with vowels letter and consonants letter if match then we have to increment the count.
char ch=n.charAt(i);
if (ch == ‘a’ | | ch == ‘e’ | | ch == ‘i’ | | ch == ‘o’ | | ch == ‘u’ | | ch == ‘A’ | | ch == ‘E’ | | ch == ‘I’ | | ch == ‘O’ | | ch == ‘U’)
v++;

How do you count special characters in Java?

To find the special character in java we can do it in three ways. First way –> we have to compare each character like as constant and vowels. Second way –> Every symbol has a special number so compare the symbol with a unique number and if it belongs to that number then count it. Third way –> This is a very simple step, We have to print all count of characters (constant and vowels), numbers, spaces and then print all others as special characters.

How do you count the number of digits in a string in Java?

To count the number of digits we can this way:
n = s.nextLine(); // Take input
for (int i=0;i < n.length();i++) // user for loop to get each characters
{
char ch=n.charAt(i);
// compare each character with digit if it is Trure then increment digit count.
if (ch == ‘0’ | | ch == ‘1’ | | ch == ‘2’ | | ch == ‘3’ | | ch == ‘4’ | | ch == ‘5’ | | ch == ‘6’ | | ch == ‘7’ | | ch == ‘8’ | | ch == ‘9’)
d++;

Recommended Post:

Get Salesforce Answers

Leave a Reply

%d bloggers like this: