C语言查找从键盘上输入的三个字符串中最大的字符串
时间: 2023-08-14 15:07:22 浏览: 94
可以使用strcmp()函数实现字符串大小比较,可以使用循环来逐个比较输入的三个字符串,找出最大的字符串。
下面是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 50
int main() {
char str1[MAX_LEN], str2[MAX_LEN], str3[MAX_LEN];
printf("请输入三个字符串:\n");
scanf("%s %s %s", str1, str2, str3);
// 比较str1和str2
char max_str[MAX_LEN];
if (strcmp(str1, str2) > 0) {
strcpy(max_str, str1);
} else {
strcpy(max_str, str2);
}
// 再比较max_str和str3
if (strcmp(max_str, str3) < 0) {
strcpy(max_str, str3);
}
printf("最大的字符串是:%s\n", max_str);
return 0;
}
```
在这个示例代码中,我们定义了一个`MAX_LEN`常量来表示输入的字符串的最大长度,然后使用`scanf()`函数从键盘上读入三个字符串。接着,我们使用`strcmp()`函数来比较两个字符串的大小,如果第一个字符串比第二个字符串大,就把第一个字符串复制到`max_str`中,否则把第二个字符串复制到`max_str`中。最后,再用`strcmp()`函数比较`max_str`和第三个字符串的大小,如果第三个字符串比`max_str`大,就把第三个字符串复制到`max_str`中。最终,输出`max_str`即可。