C语言比较两个字符串是否相同

在不使用string.h中的内置函数的情况下

cpp 复制代码
#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之中的

cpp 复制代码
#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){//如果两个字符串的长度和内容都一样,返回0
        printf("相等");
    }
    else{
        printf("不等");
    }//如果两个字符串的长度和内容有一个不一样,就判断为不等
}

内置函数strcmp

相关推荐
norlan_jame18 小时前
C-PHY与D-PHY差异
c语言·开发语言
czy878747519 小时前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
m0_5312371719 小时前
C语言-数组练习进阶
c语言·开发语言·算法
Z9fish1 天前
sse哈工大C语言编程练习23
c语言·数据结构·算法
代码无bug抓狂人1 天前
C语言之单词方阵——深搜(很好的深搜例题)
c语言·开发语言·算法·深度优先
CodeJourney_J1 天前
从“Hello World“ 开始 C++
c语言·c++·学习
枫叶丹41 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
with-the-flow1 天前
从数学底层的底层原理来讲 random 的函数是怎么实现的
c语言·python·算法
Sunsets_Red1 天前
P8277 [USACO22OPEN] Up Down Subsequence P 题解
c语言·c++·算法·c#·学习方法·洛谷·信息学竞赛
小刘爱玩单片机1 天前
【stm32简单外设篇】- 测速传感器模块(光电)
c语言·stm32·单片机·嵌入式硬件