首先我们需要知道,比较字符串是使用函数strcmp。
1. 函数原型
int strcmp(const char *s1,const char *s2);
2. 功能
比较字符串s1和s2。
3. 头文件
#include <string.h>
4. 返回值
自左向右逐个按照ASCII码值进行比较,直到出现不同的字符或遇’\0’为止。
如果返回值 < 0,则表示 s1 小于 s2。
如果返回值 > 0,则表示 s1 大于 s2。
如果返回值 = 0,则表示 s1 等于 s2。
所以我们定义两个数组,然后进行比较即可。
#include<stdio.h>
#include<string.h>
int main()
{
char a[100] = { 0 };
char b[100] = { 0 };
scanf("%s %s", &a, &b);
if (strcmp(a, b) == 0)
{
printf("same");
}
else
{
printf("different");
}
return 0;
}