Count the number of letters, digits and special symbol using java program
String str = "abcd_123#";
This sentence containing 4 letters 3 digits 2 Special numbers
// 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");
}
}
This sentence containing 4 letters 3 digits 2 Special numbers
// 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"); } } }
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.
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.