文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C程序习题-对字符数组进行排序[10.11]

C程序习题-对字符数组进行排序[10.11]

时间:2010-08-22  来源:chengxiaopeng

    在主函数中输入5个等长的字符串。用另一个函数对它们进行排序。然后在主函数输出这5个已排好序的字符串。     根据书中的对整数的选择排序法,修改为对字符串的选择排序法。即可完成。代码如下:  

#include <stdio.h>

void sort(char (*)[6],int);
int main(int argc,char *argv[])
{
    char str[5][6];
    char (*p)[6] = str;
    int i;
    printf("please input 5 strings(5 character):\n");
    for (i = 0; i < 5; i++)
    {
        scanf("%s",*p++);
    }
    p = str;
    sort(p,5);
    printf("now,the sequence is :\n");
    for (i = 0; i < 5 ;i++)
    {
        printf("%s\n",str[i]);
    }
    
    system("pause");
    return 0;
}

void sort(char (*string)[6],int n)
{
     int i,j,k;
     char temp[6];
     for (i = 0; i < n - 1; i++)
     {
         k = i;
         for(j = i + 1;j < n; j++)
         {
               if (strcmp(string[k],string[j]) > 0)
               {
                  k = j;
               }
         }
         if (k != i)
         {
            strcpy(temp,string[i]);
            strcpy(string[i],string[k]);
            strcpy(string[k],temp);
         }
     }
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载