源程序:
import java.util.Scanner;
public class p78_10 {
public static void main(String[] args)
{
int n_char=0,n_digit=0,n_other=0;
char []a=new char[100];
Scanner reader=new Scanner(System.in);
System.out.print("请输入一串字符(包含字母、数字和特殊字符):");
String str1=reader.next();
for(int i=0;i<str1.length();i++)
{
a[i]=str1.charAt(i);
if(a[i]>='a'&&a[i]<='z' || a[i]>='A'&&a[i]<='Z')
n_char++;
else if(a[i]>='0'&&a[i]<='9')
n_digit++;
else
n_other++;
}
System.out.println("字母的个数:"+n_char+" ");
System.out.println("数字的个数:"+n_digit+" ");
System.out.println("特殊字符的字母的个数:"+n_other+" ");
System.out.println("全部字符个数:"+str1.length()+" ");
reader.close();
}
}
运行结果: