方法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;
}