In Java, to print the frequency of alphabets, digits, and special characters in a string, we can use loop constructs such as a for loop or a while loop.
For instance, if the input string is “Print#$21”, then in the given string there are: 1 uppercase letter, 4 lowercase letters, 2 digits, and 2 special characters.
In this article we will learn to write a code to print the frequency of alphabets, digits, and special characters in java through an example, detailed logic and program explanation for better understanding.
📝 Problem Statement
We need to write a java program to print the frequency of alphabets, digits and special character in a string. The program should take input string which includes alphabets (both upper and lower case), digits, and special characters and print the frequency of each category respectively. This will be achieved in out program. Let’s see an example,
Example:
For a given input String str “Hi, is!11 BeC@reful”, the output should be: uppercase letters count – 3, lowercase letters count – 9, digits count – 2, and special characters count – 5.
Explanation:
Input String str: Hi, is!11 BeC@reful
Uppercase letters count:
Uppercase letters in str: 'H', 'B' and 'C'
Therefore, upperCase letters count : 3
LowerCase letters count:
Lowercase letters in str: 'i', 'i', 's', 'e', 'r', 'e', 'f', 'u', 'l'
Therefore, lowerCase letters count: 9
Digits count:
Digits in str: '1' and '1'
Therefore, digits count: 2
Special characters count:
Special characters in str: ', ' , ' ' (space), '!', ' ' (space), '@'
Therefore, special character count: 5
Hence, for the input string "Hi, is!11 BeC@reful", the counts are:
Uppercase letters: 3
Lowercase letters: 9
Digits: 2
Special characters: 5
💡 Logic to print the frequency of alphabets, digits and special character in a string
- When using a while or a for loop, to print frequency of alphabets, digits and special characters in a string, the first step is to initializing the string (str).
- Initialize uppercase count, lowercase count, digits count, and special characters count to 0. They will store the respective outputs later in the program.
- Using toCharArray() method assign str to a character array variable characters.
- Using a loop (for or while), iterate through the end of the array from index 0.
- Inside the loop, assign characters at current index to the character variable ch.
- Using if else statements, check if character is uppercase, increment the uppercase count, else if the character is lowercase, increment the lowercase count, else if the character is digit then increment the digit count. Finally, if there is all these three conditions fails finally increment special characters count.
Java Program to find the frequency of alphabets, digits and special character in a string
✅ Program 1: Using for loop
In the below program we are using for loop to the frequency of alphabets, digits and special character in a string.
import java.util.Scanner;
public class OccuranceCharactersString {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the text :");
String str = s.nextLine();
System.out.println("input: " + str);
//In alphabets we have upper case and lower case
int upperCaseLettersCount=0;
int lowerCaseLettersCount=0;
int digitsCount=0;
int specialCharacterCount=0;
char [] characters =str.toCharArray();
for(int i=0;i<characters.length;i++) {
int ch =characters[i];
if(Character.isUpperCase(ch))
upperCaseLettersCount++;
else if(Character.isLowerCase(ch))
lowerCaseLettersCount++;
else if(Character.isDigit(ch))
digitsCount++;
else
specialCharacterCount++;
}
System.out.println("output :");
System.out.println("upperCase Letters Count :"+upperCaseLettersCount);
System.out.println("lowerCase Letters Count :"+lowerCaseLettersCount);
System.out.println("digits count :"+ digitsCount);
System.out.println("special character count :"+specialCharacterCount);
s.close();
}
}
Output:
Enter the text :
HI,java@1$@3+
input: HI,java@1$@3+
output :
upperCase Letters Count :2
lowerCase Letters Count :4
digits count :2
special character count :5
🧠 Explanation:
- The program imports the
Scanner
class fromjava.util
. - It defines a class named
OccuranceCharactersString
. - Inside the class, there’s a
main
method, which is the entry point of the program. - It creates a new
Scanner
object nameds
to read input from the console. - The program asks the user to enter a text by printing “Enter the text:” and reads it into a
String
variable named str. - It prints the entered text to the console.
- The program initializes variables to count occurrences of uppercase letters, lowercase letters, digits, and special characters.
- It converts the input str into a character array using the
toCharArray()
method. - It iterates through each character in the array and checks its type using the
Character
class methodsisUpperCase()
,isLowerCase()
,isDigit()
. - Depending on the type of character, the respective count variable is incremented.
- Finally, it prints the counts of uppercase letters, lowercase letters, digits, and special characters to the console.
- It closes the
Scanner
objects
to release system resources.
✅ Program 2: Using while loop
In the below program we are using while loop to the frequency of alphabets, digits and special character in a string.
import java.util.Scanner;
public class OccuranceCharactersString{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the text :");
String str = s.nextLine();
System.out.println(str);
//In alphabets we have upper case and lower case
int upperCaseLettersCount=0;
int lowerCaseLettersCount=0;
int digitsCount=0;
int specialCharacterCount=0;
char [] characters =str.toCharArray();
int i=0;
while(i<characters.length) {
int ch =characters[i];
if(Character.isUpperCase(ch))
upperCaseLettersCount++;
else if(Character.isLowerCase(ch))
lowerCaseLettersCount++;
else if(Character.isDigit(ch))
digitsCount++;
else
specialCharacterCount++;
i++;
}
System.out.println("output :");
System.out.println("upperCase Letters Count :"+upperCaseLettersCount);
System.out.println("lowerCase Letters Count :"+lowerCaseLettersCount);
System.out.println("digits count :"+ digitsCount);
System.out.println("special character count :"+specialCharacterCount);
s.close();
}
}
Output:
Enter the text :
J@vais123@#4(Bh
input: J@vais123@#4(Bh
output :
upperCase Letters Count :2
lowerCase Letters Count :5
digits count :4
special character count :4
🧠 Explanation:
- The program imports the
Scanner
class fromjava.util
. - It defines a class named
OccuranceCharactersString
. - Inside the class, there’s a
main
method, which is the entry point of the program. - It creates a new
Scanner
object nameds
to read input from the console. - The program asks the user to enter a text by printing “Enter the text:” and reads it into a
String
variable named str. - It prints the entered text to the console.
- The program initializes variables to count occurrences of uppercase letters, lowercase letters, digits, and special characters.
- It converts the input str into a character array using the
toCharArray()
method. - It initializes an index
i
to iterate through the characters array. - It uses a
while
loop to iterate through each character in the array and checks its type using theCharacter
class methodsisUpperCase()
,isLowerCase()
,isDigit()
. - Depending on the type of character, the respective count variable is incremented.
- Finally, it prints the counts of uppercase letters, lowercase letters, digits, and special characters to the console.
- It closes the
Scanner
objects
to release system resources.
Conclusion
The programs we implemented here gives us to print the frequency of alphabets, digits and special character in a string using a for loop and a while loop.