- package Practice;
- class CharacterSorter
- {
- public static String CharacterSort(String s)
- {
- int[] count=new int[4];
- char[] chs=s.toCharArray();
- for(char ch:chs)
- {
- if(ch==' ')count[0]++;
- else if ('0'<=ch&ch<='9')
- count[1]++;
- else if(('a'<=ch&ch<='z')||
- ('A'<=ch&ch<='Z'))
- count[2]++;
- else
- count[3]++;
- }
- int sum=count[0]+count[1]+count[2]+count[3];
- return "字符总数:"+sum+"个,空格数个数:"+count[0]+"个,数字个数:"
- +count[1]+"个,字母个数:"+count[2]+"个,其他字符个数:"+count[3];
- }
- }
- public class TestCharacterSorter
- {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String str=" sd fhg ui sd 14 6SFG5434/.x,.a";
- System.out.println(CharacterSorter.CharacterSort(str));
- }
- }
- //打印结果
- //字符总数:32个,空格数个数:7个,数字个数:7个,字母个数:14个,其他字符个数:4。
复制代码 |