C语言根据国家英文首字母进行排序

C语言根据国家英文首字母进行排序

#include <stdio.h>
#include <string.h>
#define MAX_LEN 10
#define N       150
void SortString(char str[][MAX_LEN],int n);
int main()
{
    int i,n;
    char name[N][MAX_LEN];
    printf("How many countries ?
" );
    scanf("%d",&n);
    getchar();
    printf("Input their names:
");
    for(i=0;i<n;i++){
        gets(name[i]);
    }
    SortString(name,n);
    printf("Sorted results:
");
    for(i=0;i<n;i++){
        puts(name[i]);
    }
}
void SortString(char str[][MAX_LEN],int n)
{
    int i,j;
    char temp[MAX_LEN];
    //遍历所有国家名称
    for(i=0;i<n-1;i++)  
    {
        //遍历每个国家的首字母
        for(j=i+1;j<n;j++)
        {
            if(strcmp(str[j],str[i])<0)
            {
                strcpy(temp,str[i]);
                strcpy(str[i],str[j]);
                strcpy(str[j],temp);
            }
        }
    }
}

效果截图
效果截图

博客园:https://www.cnblogs.com/newtol 微信公众号:Newtol 【转发请务必保留原作者,否则保留追责权利】
原文地址:https://www.cnblogs.com/newtol/p/10159142.html