C语言 用指针数组实现字符串的排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char *s[], int len)
{
int i,j;
char *temp = (char *)malloc(sizeof(char) * 30);
for(i = 0; i < len-1; i++){
for(j = 0; j < len-1-i; j++){
if((strcmp(s[j], s[j+1])) > 0){
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
}
}
free(temp);
}
int main()
{
int k;
char *s[] = {"hello", "world", "abcde", "boy"};
int size = sizeof(s)/sizeof(s[0]);
sort(s ,size);
for(k = 0; k < size; k++){
printf("%s ",s[k]);
}
printf("\n");
return 0;
}