输入描述:每行有两个空格分开的字符串,第一个为密码,第二个为重复密码
输出描述:每组输出第一个字符串(“same”或“different”)。
具体代码:
#include <stdio.h>
#include <string.h>
int main()
{
char password1[100] = {0};
char password2[100] = {0};
scanf("%s %s",password1,password2);
if(strcmp(password1,password2)==0)
printf("same\n");
else
printf("different\n");
return 0;
}
字符串的比较不能直接使用“==”来进行比较,应当使用strcmp库函数进行比较。
strcmp是C语言中的字符串比较函数,用于比较两个字符串的大小关系。它的原型如下:
int strcmp(const char* str1, const char* str2);
strcmp函数接受两个参数,即要比较的两个字符串str1和str2,然后返回一个整数值,表示它们的大小关系。具体而言:
如果返回值为0,表示str1和str2相等;
如果返回值小于0,表示str1小于str2;
如果返回值大于0,表示str1大于str2。
下面是一个使用strcmp函数的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello";
char str2[] = "world";
int result = strcmp(str1, str2);
if (result == 0) {
printf("str1 and str2 are equal\n");
} else if (result < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}
输出结果为:
str1 is less than str2
在这个示例中,我们比较了两个字符串"hello"和"world",并通过strcmp函数判断它们的大小关系。由于"hello"小于"world",所以strcmp返回值小于0,输出结果为"str1 is less than str2"。