Define a class to accept a String and print the number of digits, alphabets and special characters in the string. Example: S = "KAPILDEV@83"
Advertisements
प्रश्न
Define a class to accept a String and print the number of digits, alphabets and special characters in the string.
| Example: | S = "KAPILDEV@83" |
| Output: | Number of digits - 2 ' Number of Alphabets - 8 Number of Special characters - 1 |
थोडक्यात उत्तर
Advertisements
उत्तर
import jaya.util.*;
public class CountAlpDigiSpl {
public static void main(String[] args) {
String aldisp_str;
int i, alph, digi, spl;
alph = digi = spl = 0;
char ch;·'
Scanner sc= new Scanner(System.in);
System.out.print("\nPlease Enter Alpha Numeric Special String :
");
aldisp_str = sc.nextLine();
for(i = 0; i < aldisp_str.length(); i++)
{
ch= aldisp_str.charAt(i);
if(ch >= 'a' && ch<= 'z' | | ch >= 'A' &&
ch <= 'Z' ) {
alph++;
}
else if(ch >= '0' && ch<= '9') {
digi++;
}
else {
spl++;
}
}
System.out.println("\nNumber of Alphabet
Characters = " + alph);
System.out.println("Number of Digit
characters. = " + digi);
System.out.println("Number of Special
Characters = " + spl);
}
}shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
