字符串内容比较后交换内容输出
时间: 2023-12-26 18:06:10 浏览: 23
可以使用字符串比较函数strcmp()和strcpy()来实现字符串内容比较后交换内容输出。
下面是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], temp[100];
printf("Enter the first string: ");
gets(str1);
printf("Enter the second string: ");
gets(str2);
if (strcmp(str1, str2) > 0) {
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
printf("After swapping:\n");
printf("First string: %s\n", str1);
printf("Second string: %s\n", str2);
return 0;
}
```
在上面的代码中,我们首先使用gets()函数从标准输入中获取两个字符串。然后,我们使用strcmp()函数比较这两个字符串。如果str1大于str2,则我们使用strcpy()函数将str1的值复制到temp中,然后将str2的值复制到str1中,最后将temp的值复制到str2中。最后,我们使用printf()函数输出交换后的字符串。