當前位置:成語大全網 - 新華字典 - 設計壹個函數實現對10個英文字符串進行字典排序

設計壹個函數實現對10個英文字符串進行字典排序

//設計壹個函數實現對10個英文字符串進行字典排序

#include <stdio.h>

#include <string.h>

void sort(char str[][100])

{

int i, j;

char temp[100];

//冒泡排序

for(i = 0; i < 9; i++)

{

for(j = i+1; j < 10; j++)

{

if(strcmp(str[i], str[j]) == 1)

{

strcpy(temp, str[i]);

strcpy(str[i], str[j]);

strcpy(str[j], temp);

}

}

}

}

int main()

{

char str[10][100];

int i;

printf("Please input a 10 string: \n");

for(i = 0; i < 10; i++)

{

printf("string %d : ", i+1);

fgets(str[i], 100, stdin);

}

sort(str);

for(i = 0; i < 10; i++)

printf("%s\n", str[i]);

return 0;

}

PS:若有不明白的地方,可以追問