输入描述:
输入有两行,第一行n 第二行是n个字符串,字符串之间用空格隔开
输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格
输入例子1:
5 c d a bb e
输出例子1:
a bb c d e
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STR_LEN 20
int strCmp(const void *a, const void*b)
{
char *s1 = *(char**)a;
char *s2 = *(char**)b;
return strcmp(s1, s2);
}
int main()
{
int n;
scanf("%d",&n);
char **s = (char **)malloc(sizeof(char *) * n);
memset(s, 0, sizeof(char *) * n);
for(int i = 0; i < n; i++){
s[i] = (char *)malloc(sizeof(char ) * STR_LEN);
memset(s[i] ,0, sizeof(char) * STR_LEN);
scanf("%s", s[i]);
}
qsort(s, n, sizeof(char*), strCmp);
for(int i = 0; i < n; i++){
printf("%s",s[i]);
free(s[i]);
if (i < n) {
printf(" ");
} else {
printf("\n");
}
}
free(s);
return 0;
}