统计文章中的字符串(二)

问题:有篇文章“Alibaba will invest 4.66 billion yuan ($693 million) in STO Express, and will explore further cooperation in logistics technology, express terminals and new retail logistics with STO Express after the transaction, according to an announcement by the latter company.";
     要求统计文章里每种字符的数目

思路:应当考虑空格 逗号,句号,等符号的个数

第一步: 将文章转化为字符串

第二步:利用强制转换,将字符串中每种字符的数值出现的次数统计在一个空数组里

第三步:利用字符数组遍历,以及字符转换,输出

String a = "Alibaba will invest 4.66 billion yuan ($693 million) in STO Express, and will explore further cooperation in logistics technology, express terminals and new retail logistics with STO Express after the transaction, according to an announcement by the latter company.";
        int []b = new int[256];
        for(int i=0; i<a.length(); i++)
        {
            int c = (int)a.charAt(i);
            b[c]++;
        }
        
        for(int j=0; j<256; j++)
        {
            if(b[j]!=0)
            {
                char d = (char)j;
                System.out.printf("%c:%d\n", d, b[j]);
            }
        }

效果展示

 :38    
$:1    
(:1    
):1    
,:3    
.:2    
3:1    
4:1    
6:3    
9:1    
A:1    
E:2    
O:2    
S:2    
T:2    
a:16    
b:4    
c:9    
d:3    
e:19    
f:2    
g:4    
h:5    
i:20    
l:16    
m:4    
n:21    
o:15    
p:6    
r:13    
s:13    
t:18    
u:3    
v:1    
w:4    
x:4    
y:4    


posted @ 2019-03-13 22:15  布朗克的黑猫  阅读(135)  评论(0编辑  收藏  举报