构造一个函数,完成英文单词的排序,主函数实现单词的输入和排序结果的输出。C语言函数
时间: 2024-05-21 22:10:15 浏览: 38
排序算法,用C语言来做的
#include <stdio.h>
#include <string.h>
void sortWords(char words[][50], int n) {
int i, j;
char temp[50];
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (strcmp(words[i], words[j]) > 0) {
strcpy(temp, words[i]);
strcpy(words[i], words[j]);
strcpy(words[j], temp);
}
}
}
}
int main() {
int n, i;
char words[100][50];
printf("Enter the number of words: ");
scanf("%d", &n);
printf("Enter %d words:\n", n);
for (i = 0; i < n; i++) {
scanf("%s", words[i]);
}
sortWords(words, n);
printf("\nSorted words:\n");
for (i = 0; i < n; i++) {
printf("%s\n", words[i]);
}
return 0;
}
阅读全文