#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
char names[][20] = {"zhang", "zhao", "jiang", "peng", "dong", "do", "zhan"};
int count = sizeof(names) / sizeof(names[0]);int flag = 1;
for (int i = 0; i < count - 1 && 1 == flag; i++) {
flag = 0;
for (int j = 0; j < count - i - 1; j++) {
if (strcmp(names[j], names[j + 1]) > 0) {
char temp[20] = {'\0'};
strcpy(temp, names[j]); // strcpy() 一维数组的复制
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], temp);
flag = 1;
}
}
}
for (int i = 0; i < count; i++) {
puts(names[i]);
}
return 0;
}
本文通过一个具体的C语言程序实例介绍了如何使用冒泡排序算法对字符串数组进行排序。该程序首先定义了一个包含多个字符串的数组,然后利用双重循环实现了冒泡排序,并在每一轮比较中使用了strcmp函数来确定字符串的大小关系。最后,程序输出了排序后的字符串数组。
&spm=1001.2101.3001.5002&articleId=37892661&d=1&t=3&u=81de7e2b62744bc5985c53ee4496cfc7)
1万+

被折叠的 条评论
为什么被折叠?



