输入三个字符串,按又小到大的顺序输出

方法1:

#include <iostream>
#include <string>
#pragma warning(disable:4996)
using namespace std;
int main()
{
	char str1[20], str2[20], str3[20];
	cout << "请输入三个字符串:" << endl;
	gets_s(str1,20); gets_s(str2,20); gets_s(str3,20);
	if (strcmp(str1, str2) > 0)swap(str1, str2);
	if (strcmp(str1, str3) > 0)swap(str1, str3);
	if (strcmp(str2, str3) > 0)swap(str2, str3);
	cout << "the order is :" << endl;
	cout << str1 << " " << str2 << " " << str3 << endl;
	return 0;

}
void swap(char* p1, char* p2)
{
	char temp[20];
	strcpy(temp, p1); strcpy(p1, p2); strcpy(p2, temp);

}

方法2:

#include <iostream>
#include <string>
#pragma warning(disable:4996)
using namespace std;
int main()
{
	void swap(string&, string&);
	string str1 = "                                         ";
	string str2 = "                                         ";
	string str3 = "                                         ";
	char* p1 = &str1[0];
	char* p2 = &str2[0];
	char* p3 = &str3[0];
	cout << "请输入三个字符串:" << endl;
	gets_s(p1,10); gets_s(p2,10); gets_s(p3,10);
	if (str1 > str2) swap(str1, str2);
	if (str1 > str3) swap(str1, str3);
	if (str2 > str3) swap(str2, str3);

	cout << endl << "the order is:" << endl;
	cout << str1 << " " <<  str2 << " " << str3 << endl;
	return 0;

}
void swap(string& a, string& b)
{
	string temp;
	temp = a; a = b; b = temp;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值