帮我写一个判断字符串大小的代码
时间: 2023-04-02 09:02:47 浏览: 57
好的,以下是一个简单的判断字符串大小的代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
int result;
printf("请输入第一个字符串:");
scanf("%s", str1);
printf("请输入第二个字符串:");
scanf("%s", str2);
result = strcmp(str1, str2);
if (result > ) {
printf("第一个字符串大于第二个字符串\n");
} else if (result < ) {
printf("第一个字符串小于第二个字符串\n");
} else {
printf("两个字符串相等\n");
}
return ;
}
```
希望能对您有所帮助。
相关问题
帮我用C语言写一个判断字符串大小的代码
好的,以下是判断字符串大小的 C 语言代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
int result;
printf("请输入第一个字符串:");
scanf("%s", str1);
printf("请输入第二个字符串:");
scanf("%s", str2);
result = strcmp(str1, str2);
if (result < ) {
printf("第一个字符串比第二个字符串小\n");
} else if (result > ) {
printf("第一个字符串比第二个字符串大\n");
} else {
printf("两个字符串相等\n");
}
return ;
}
```
希望能够帮到您!
strcmp判断两个字符串大小的代码
以下是使用strcmp()函数判断两个字符串大小的示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "hello";
char str2[20] = "world";
int result = strcmp(str1, str2); // 比较两个字符串大小
if (result < 0) {
printf("%s is smaller than %s\n", str1, str2);
} else if (result > 0) {
printf("%s is larger than %s\n", str1, str2);
} else {
printf("%s and %s are equal\n", str1, str2);
}
return 0;
}
```
输出结果:
```
hello is smaller than world
```