C语言比较两个字符串是否相同
在不使用string.h中的内置函数的情况下
#include <stdio.h>
#include <string.h>
void main(){char arr1[]="hello world";char arr2[]="hello world";int i,a=0;if(strlen(arr1)!=strlen(arr2)){print("不相等");return 0;}for(i=0;arr1[i]!='\0';i++){if(arr1[i]!=arr2[i]) a++;}if(a==0)){printf("相等");}else{printf("不等");}return 0;}
当然我们也可以使用string.h之中的
#include <stdio.h>
#include <string.h>
void main(){char arr1[20]="hello world";char arr2[20]="hello worla";int a=strcmp(arr1,arr2);//strcmp会判断;两个字符串,if(a==0){//如果两个字符串的长度和内容都一样,返回0printf("相等");}else{printf("不等");}//如果两个字符串的长度和内容有一个不一样,就判断为不等
}
内置函数strcmp