给定程序中,函数fun的功能是:对形参ss所指字符串数组中的M个字符串按长度由短到长进行排序。ss所指字符串数组中共有M个字符串,且串长
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include #include #define M 5 #define N 20 void fun(char (*ss)[N]) { int i, j, k, n[M]; char t[N]; for(i=0; i for(j=___1___; j if(k!=i) { strcpy(t,ss[i]); strcpy(ss[i],ss[k]); strcpy(ss[k],___3___); n[k]=n[i]; } } } main() { char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"}; int i; printf("\nThe original strings are :\n"); for(i=0; i fun(ss); printf("\nThe result :\n"); for(i=0; i 解题思路: 本题是考察考生如何对字符串中的字符进行逆序操作。给出的程序使用了一个临时变量b 的字符串,使用for循环语句把原字符串的字符从尾部依次赋给临时变量b(从头开始)中,循环结束后,再把临时变量b的内容重新复制给原字符串变量即可。 参考答案: fun ( char *s ) { char b[N] ; int i = 0, j ; memset(b, 0, N) ; for(j = strlen(s) - 1 ; j >= 0 ; j--) b[i++] = s[j] ; strcpy(s, b) ; } ※※※※※※※※※※※※※※※※※※※※※※※※※