三个字符串比较大小,stycmp( ) .
时间: 2024-05-11 18:18:04 浏览: 16
可以使用strcmp()函数来比较两个字符串的大小。它将返回一个整数值,表示两个字符串的大小关系。如果字符串1小于字符串2,则返回一个负数,如果它们相等,则返回0,如果字符串1大于字符串2,则返回一个正数。
下面是比较三个字符串大小的示例代码:
```
#include <stdio.h>
#include <string.h>
int main()
{
char str1[50], str2[50], str3[50];
int result1, result2;
printf("Enter three strings:\n");
gets(str1);
gets(str2);
gets(str3);
result1 = strcmp(str1, str2);
result2 = strcmp(str2, str3);
if(result1 < 0 && result2 < 0)
printf("%s is the largest string\n", str3);
else if(result1 > 0 && result2 > 0)
printf("%s is the largest string\n", str1);
else
printf("%s is the largest string\n", str2);
return 0;
}
```
在这个示例中,我们使用了gets()函数来获取三个字符串,然后使用strcmp()函数来比较两个字符串的大小。最后,我们使用if-else语句来确定哪个字符串最大,并将其输出。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)