题目描述
主函数接收三个字符串,利用swap函数交换字符串,并按从小到大的顺序输出
函数原型:void swap(char*, char*);
解法
利用strcmp函数比较字符串的大小
代码示例
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main(void)
{
void swap(char*, char*);
char str1[20], str2[20], str3[20];
printf("input three line:\n");
gets(str1);
gets(str2);
gets(str3);
if (strcmp(str1, str2) > 0)swap(str1, str2);
if (strcmp(str1, str3) > 0)swap(str1, str3);
if (strcmp(str2, str3) > 0)swap(str2, str3);
printf("%s\n%s\n%s\n", str1, str2, str3);
return 0;
}
void swap(char* p1, char* p2)
{
char p[20];
strcpy(p, p1); strcpy(p1, p2); strcpy(p2, p);
}