字符数组排序

字符数组排序,第1张

字符数组排序

输入五个国家的名称按字母顺序排列输出。 

#define _CRT_SECURE_NO_WARNINGS \Microssoft Visual Studio非搞得跟别人不一样
#include 
#include 
int main()
{
	char country_name[5][20], temp[20];
	int i, j, p;
	printf("input country's name:n");
	for (i = 0; i < 5; i++)
	{
		gets(country_name[i]);
	}
	printf("n");
	for (i = 0; i < 5; i++)
	{
		p = i;
		strcpy(temp, country_name[i]);
		for (j = i + 1; j < 5; j++)
		{
			if (strcmp(country_name[j], temp) < 0)
			{
				p = j;
				strcpy(temp, country_name[j]);
			}
		}
		if (p != i)
		{
			strcpy(temp, country_name[i]);
			strcpy(country_name[i], country_name[p]);
			strcpy(country_name[p], temp);
		}
		puts(country_name[i]);
	}
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5714476.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存