Code is copied!
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
Solution:
import java.util.*;
class Frequency
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String");
String s = sc.nextLine();
int d=0, a=0, sp=0;
for(int i=0; i< s.length(); i++)
{
char c = s.charAt(i);
if(Character.isDigit(c))
d++;
else if(Character.isLetter(c))
a++;
else
sp++;
}
System.out.println("Number of digits - "+d);
System.out.println("Number of alphabets - "+a);
System.out.println("Number of Special characters - "+sp);
}
}