void sortArr(char *p[], int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (strcmp(*(p + j), *(p + j + 1)) > 0) {
char *temp = NULL;
temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
}
}//对字符串排序(升序)
void outputCharArr(char *p[], int count) {
for (int i = 0; i < count; i++) {
printf("%s\n", p[i]);
}
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (strcmp(*(p + j), *(p + j + 1)) > 0) {
char *temp = NULL;
temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
}
}//对字符串排序(升序)
void outputCharArr(char *p[], int count) {
for (int i = 0; i < count; i++) {
printf("%s\n", p[i]);
}
}//输出字符串
char *str[10] = {"liyiyiyi", "wangerer", "zhangsan", "lisisisi", "wangwuwu", "zhaoliul", "wangqiqi", "chenbaba", "zhoujiuj", "xusongss"};
/*
char *str[10] = {0};
printf("请输入10个字符串\n");
inputCharArr(str, 10);
*/ //调用函数用scanf输入
sortArr(str, 10);
outputCharArr(str, 10);
本文介绍了一个简单的C语言程序,该程序使用冒泡排序算法对字符串数组进行升序排序,并展示了如何输出排序后的字符串。通过具体示例代码,读者可以了解到如何在C语言中操作字符串数组,包括初始化、排序及打印输出。
7090

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



