主题:输入n个字符串,按照一定的规则按升序排序输出
我的排序规则:根据字符串中英语字符的值之和
# include stdio.h # include string.h # include stdlib.h # include ctype.h # define maxnum 10 # define maxlen 20 intmystrcmp () } for(I=0; Istrlen(S1; I//计算字符串中所有英语字符之和{if(isalpha(S1[I] ) {count1=s1[i]; }for(I=0; Istrlen(S2; I () if ) Isalpha ) S2[I] ) {count2=s2[i]; } }返回计数1 -计数2; //升序}voidstrsort(intn ) {int j=n; char **s; s=(char** ) malloc ) sizeof ) char* ) n ); int i=0; while(--j=0) if(null==(*(sI )=(char* ) malloc(sizeof ) char ) *MAXLEN ) ) ({printf ) )内存分配}scanf('%s”,s[i ]; //gets(s[I]; 两者的输入差异在前面的文章中已经叙述过了,请注意差异}for(intk=0; kn-1; 基于k//冒泡的序列化for(intt=0; tn-k-1; t () if ) mystrcmp(s[t],s[t 1] )0) {char temp[MAXLEN]; srcpy(temp,s[t] ); strcpy(s[t],s[t 1]; strcpy(s[t1],temp ); }for(I=0; in; I//打印已排序的字符串{printf(%s(n ),s[i] ); }}int main () ) {int n=0; scanf('%d ',n ); if(n==0||nmaxnum ) {printf ) )非法输入! n '; 返回0; } printf (请输入“% d个字符串n ',n ); strsort(n; 返回0; }结果:
注意事项:
1.scanf输入字符串遇到空格、制表符、换行符时作为一次输入结束; 如果要输入有空格的字符串,请单击gets (参见上一篇文章);
2 .字符串赋值时使用strcpy,不要用指针直接赋值。