First, convert the string to a character array, and then count each of the letters, or the numbers each time you iterate through the array.
package yaxin; public class Countstring {public static void main (string[] args) {String string= "abcedfgaaaabbbccccBBBCCCEEE22334455
";
Char Chs[]=string.tochararray ();//convert to char array int count=0;
System.out.println ("string:" +string+ "small and medium writing letters are:"); for (Char ch= ' a ';ch< ' z '; ch++) {count=0;//counter for (int i=0;i<chs.length;i++) {if (ch==chs[i)) Coun
t++;
} if (count!=0) System.out.println ("+ch+" has "+count+");
} System.out.println ("String: +string+" has: "); for (char ch= ' A ';ch< ' Z '; ch++) {count=0;//counter for (int i=0;i<chs.length;i++) {if (ch==chs[i)) Coun
t++;
} if (count!=0) System.out.println ("+ch+" has "+count+");
} System.out.println ("String:" +string+ "number has:"); for (char ch= ' 0 ';ch< ' 9 '; ch++) {count=0;//counter for (int i=0;i<chs.length;i++) {if (ch==chs[i)) Coun
t++;
} if (count!=0) System.out.println ("+ch+" has "+count+"); }
}
}
Results:
String: abcedfgaaaabbbccccBBBCCCEEE22334455 Small Letter has:
character A has 5 characters
B has 4 characters
C has 5 characters
D has 1 characters
E has 1
the character F has 1 characters
G has 1
strings: abcedfgaaaabbbccccBBBCCCEEE22334455 uppercase letters are:
character B has 3 characters
C has 3
E has 3 strings in the character
: the number in abcedfgaaaabbbccccBBBCCCEEE22334455 is:
2 has 2 characters
3 has 2
characters 4 has 2 characters 5 is 2