Home / Programs / Count the number of letters, digits and special symbol using java program
🚀 Programming Example

Count the number of letters, digits and special symbol using java program

👁 143 Views
💻 Practical Program
📘 Step Learning

📌 Information & Algorithm

Given Input:

 String str = "abcd_123#";

Expected Output:

This sentence containing 4 letters 3 digits 2 Special numbers

💻 Program Code

// Count the number of letters, digits and special symbol using java program

public class Main {
    public static void main(String[] args) {
        String str = "abcd_123#";
        int letterCount = 0, digitCount = 0, specialSymbolCount = 0;

		for(int i = 0; i < str.length(); i++){
			 if (Character.isLetter(str.charAt(i))) {
			 	letterCount++;
			 }else if (Character.isDigit(str.charAt(i))) {
				 digitCount++;
			} else {
             specialSymbolCount++;
        	}
		}

		System.out.println("This sentence containing "+letterCount+" letters "
		+digitCount+" digits "+specialSymbolCount+" Special numbers");
    }
}
                        

🖥 Program Output

This sentence containing 4 letters 3 digits 2 Special numbers
                            

📘 Explanation

String is alpha numeric or not?


// Count the number of letters, digits and special symbol using java program

public class Main {
    public static void main(String[] args) {
        String str = "ewerwr34534";
        int letterCount = 0, digitCount = 0, specialSymbolCount = 0;

		for(int i = 0; i < str.length(); i++){
			 if (Character.isLetter(str.charAt(i))) {
			 	letterCount++;
			 }else if (Character.isDigit(str.charAt(i))) {
				 digitCount++;
			} else {
             specialSymbolCount++;
        	}
		}

		System.out.println("This sentence containing "+letterCount+" letters "
		+digitCount+" digits "+specialSymbolCount+" Special numbers");

		if(letterCount == 0 && digitCount == 0)
		{
			System.out.println( "the string is not alpha numeric");
		}
		else
		{
			System.out.println( "the string is alpha numeric");
		}

    }
}
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.