c语言如何判断两个中文字符串相同

#includeint main(){ char j[]="是"; char w[]=""; while(w != j) { printf("输入‘是’:"); gets(w); }printf("输入正确"); return 0;}

//strcmp对中文比较识别度不高,自己编代码写
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(char *a,char *b)
{
int i=0;
while(a[i]!='\0')
{
if(a[i]!=b[i])
return 0;
i++;
}
return 1;
}
int main()
{
char a[100],b[100];
while(~scanf("%s %s",a,b))
if(strlen(a)==strlen(b))
if(cmp(a,b))
printf("yes\n");
else
printf("no\n");
else
printf("no\n");
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-23
strcmp函数不行么?
第2个回答  推荐于2016-02-19
这个需要用到string.h头文件中的strcmp命令进行比较,命令的使用方法为: strcmp(char *s1,char * s2);
当s1<s2时,返回为负数
当s1=s2时,返回值= 0
当s1>s2时,返回正数
即:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。如:
"A"<"B" "a">"A" "computer">"compare"
特别注意:strcmp(const char *s1,const char * s2)这里面只能比较字符串,不能比较数字等其他形式的参数。追问

中文不行啊 我试了好多次

追答

这个是正确代码,你试试:

#include
#include
int main()
{
char j[]="是";
char w[4]="";
while(strcmp(j,w) != 0)
{
printf("输入‘是’:");
gets(w); }
printf("输入正确\n");
return 0;}

本回答被提问者采纳
相似回答