写冒泡排序可以排序多个字符串。

来源:互联网 发布:it产品经理培训 编辑:程序博客网 时间:2024/05/29 17:00

代码实现(Visual Studio 2017)

#include <stdio.h>#include <windows.h>#include <string.h>#pragma warning(disable: 4996)void Exchange(char * str1, char *str2){    char str_e[10] = "0";    strcpy(str_e, str1);    strcpy(str1, str2);    strcpy(str2, str_e);}void sort(char str[][10]){    int i = 0;    int j = 0;    for (; i < 4; i++)    {        for (j = i; j < 5; j++)        {            if (strcmp(str[j], str[i]) < 0)            {                Exchange(str[j], str[i]);            }        }    }    return 0;}int main(){    char str[5][10] = { "ABCD","RAVW","abce","EFGA","edaae" };    sort(str);    for (int i = 0; i < 5; i++)    {        printf("%s\n", str[i]);    }    system("pause");    return 0;}
原创粉丝点击