C语言字符串运算:strlen strcmp strcpy strcat strchr strstr的理解

下面对使用 头文件 #include <string.h> 的部分函数理解 进行记录

目录

下面函数种类有:

1.关于strlen:

2.关于strcmp:

3.关于strcpy:

 4.关于strcat:

5.关于strchr:

6.关于strstr:

 补充:

1.什么是字符串常量


 下面代码均在 Clion2021 测试


下面函数种类有:

strlen strcmp strcpy strcat strchr strstr


1.关于strlen:

理解函数原型:

size_t strlen(const char *s);

用例:

#include <stdio.h>
#include <string.h>

int main() {
    char a[] = "Hello world";
    printf("%d", strlen(a));
    return 0;
}

// 运行结果
// 11

返回 s 字符串长度 (不包括字符串 结尾的 \0 )

2.关于strcmp:

理解函数原型:

int strcmp(const char *s1, const char *s2);

用例:

#include <stdio.h>
#include <string.h>

int main() {
    char s1[] = "abc";
    char s2[] = "bbc";
    printf("%d", strcmp(s1, s2));
    return 0;
}

// 运行结果
// -1

比较两个字符串,返回:

  • 0  则 s1==s2
  • 1  则 s1>s2
  • -1 则 s1<s2

3.关于strcpy:

理解函数原型:

char *strcpy(char *restrict dst, const char *restrict src);

用例:

#include <stdio.h>
#include <string.h>

int main() {
    char dst[5], src[5]="Her";
    strcpy(dst, src);
    printf("%s", dst);
    return 0;
}

// 运行结果
// Her
  • 把 src 字符串拷贝到 dst
  • 注意 目的地 dst 要有足够空间
  • restrict 表明 src 和 dst 不重叠 (Only C99)
  • 返回 dst

 4.关于strcat:

理解函数原型:

char *strcat(char *restrict s1, const char *restrict s2);

例:

#include <stdio.h>
#include <string.h>

int main() {
    char s1[20]="I love ", s2[5]="Her";
    strcat(s1, s2);
    printf("%s", s1);
    return 0;
}

// 运行结果
// I love Her
  • 把 s2 拷贝到 s1 后面,接成一个长字符串
  • 返回 s1
  • s1 必须具有足够空间

5.关于strchr:

理解函数原型:

char *strchr(const char *s, char/int c);

例:

#include <stdio.h>
#include <string.h>

int main() {
    char s1[20] = "Ilove1226";
    char c = '1';
    char *str;

    if ((str = strchr(s1, c)) == NULL) {
        printf("不存在");
    } else {
        printf("%s", str);//返回地址
    }

    return 0;
}

// 运行结果
// 1226
  • 在 s 中搜索一个int型的数据,即搜索字符
  • 返回 字符 c 在字符串str中首次出现的位置 
  • 返回 NULL 表示没有找到

6.关于strstr:

理解函数原型:

char *strstr(char *str1, const char *str2);

例:  

#include <stdio.h>
#include <string.h>

int main() {
    char s1[20] = "Ilove1226";
    char c[] = "I";
    char *str;

    if ((str = strstr(s1, c)) == NULL) {
        printf("不存在");
    } else {
        printf("%s", str);//返回地址
    }

    return 0;
}

// 运行结果
// Ilove1226
  • 在 str1 中 搜索 str2,即搜索一个字符串常量
  • 返回 str2在str1的首次出现的地址,则str2是str1的子串
  • 返回 NULL,则str2不是 str1的子串

参考 :中国大学MOOC-C语言课程


 补充:

1.什么是字符串常量

字符串常量的书写方式是一对双引号包围一串字符,如:

"Hello, world"

当一个字符串常量出现于一个表达式中时,表达式所使用的值就是这些字符所储存的地址,而不是这些字符本身。因此,你可以把字符串常量赋值给一个“指向字符的指针”,后者指向这些字符所储存的地址,如:

char *str = "Hello, world";

但是,不能把字符串常量赋值给一个字符数组,因为字符串常量的直接值是一个指针,而不是这些字符本身。简单的说,字符串常量的值是这些字符在内存中的地址。

参考:C语言中的字符串常量

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃饭用勺子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值