Count the number of letters, digits and special symbol using java program
Java Programming Language String in java (Article) String in java (Program)
26
Given Input:
String str = "abcd_123#";
Expected Output:
This sentence containing 4 letters 3 digits 2 Special numbers
Program:
// 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");
}
}
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");
}
}
}
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.
This sentence containing 4 letters 3 digits 2 Special numbers
Program:
// 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"); } }
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"); } } }
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.