[文档创建日期]2010-04-08
程序为本人原创,如须转载,请注明出处。
本程序希望实现的功能在程序行首说明:
01 // P77: 1.编写函数,读入一个字符串,统计字符串中不同字符的出现频率,挑选不同的数据集测试这个函数
02
03 // The beginning of C program: test02-01.c.
04
05 // Header files included.
06 #include <stdio.h>
07 #include <string.h>
08
09 // Macro definition.
10 #define MAX_SIZE 256
11
12 // No.1: void count (const char *array, int *result);
13 void count_num (const char *array, int *result )
14 {
15 int ASKII_num=0, len=0, i, count=0;
16 ASKII_num = array[len];
17 for ( i=0; i<MAX_SIZE; i++ )
18 {
19 count = 0;
20 len = 0;
21 while ( array[len] != '/0' )
22 {
23 ASKII_num = array[len];
24 if ( ASKII_num == i )
25 {
26 count ++;
27 }
28 len ++;
29 }
30 result[i] = count;
31 }
32 }
33
34 // Main function's declaration.
35 int main ( int argc, char *argv[] )
36 {
37 int i, result[MAX_SIZE], count=0, temp=argc-2;
38
39 if ( argc < 2 )
40 {
41 printf ( "The program is to count out the number of different characters in your puts./n" );
42 printf ( "Usage: countout + [string]/n" );
43 return (1);
44 }
45
46 // Initialise result[MAX_SIZE].
47 for ( i=0; i<MAX_SIZE; i++ )
48 {
49 result[i] = 0;
50 }
51
52 for ( i=1; i<argc; i++ )
53 {
54 count_num (argv[i], result);
55 }
56 result[32] = result[32] + temp;
57
58 for ( i=0; i<MAX_SIZE; i++ )
59 {
60 if ( result[i] != 0 )
61 {
62 count ++;
63 printf ( " ASKII code(Decimal) %3d: %3c Num: %d/n",
64 i+1, i, result[i] );
65 }
66 }
67 printf ( "There are %d different characters in the string./n", count );
68
69 return (0);
70 }
71
72 // The end of C program: test02-01.c.
73
此程序尚待改进,有个地方有问题:只能操作一个中间没有空格的连续字符串,不能操作多字符串。
大家看到了有什么更好的建议和见解,请在后面留言,谢谢 ^_^