C語言程序實現,統計字符串里面各個字符的個數在總字符個數中的比例,並打印輸出。


 1 #include<stdio.h>
 2 int main()
 3 {
 4     char *ppp= "aaassadddeeds";
 5     char c[255] = {0};//存放字符
 6     uint32 ccnt[255] = { 0 };//存放字符對應的個數
 7     char *t = NULL;
 8     t = ppp;
 9     uint32 index_i = 0;
10     uint32 count = 0;//總字符的個數
11     uint32 cnt = 0;//字符的種類數
12     while (*t != '\0')
13     {
14              /*在存儲區域內之前出現過*/
15         for (index_i = 0; index_i < cnt; index_i++)
16         {
17             if (c[index_i] == *t)
18             {
19                 ccnt[index_i]++;
20                 break;
21             }
22         }
23            /*在存儲區域內第一次出現*/
24         if (index_i >= cnt)
25         {
26             c[cnt++] = *t;
27             ccnt[index_i]++;
28 
29         }
30         t++;
31         count++;
32     }
33     printf("%d\n", debug);
34     printf("%d\n", count);
35     for (index_i = 0; index_i < cnt; index_i++)
36     {
37         printf("%c %.2f%%\n", c[index_i], float((float)ccnt[index_i] / count)*100);
38     }
39     return 0;
40 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM