C语言常用字符串函数 —— strlen(), strcpy(), strcat(), strcmp(), strspn(), strstr(), strchr()

一、strlen()函数

1.作用:返回C字符串的长度(C字符串以'\0'结尾,'\0'不会被计算在内)

2.示例:

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

int main()
{
    char str[] = "Hello, world!";
    int len = strlen(str);
    printf("The length of the string is: %d\n", len);
    return 0;
}

3.输出结果:

The length of the string is: 13

 二、strcpy()函数

1.作用:覆盖复制,将一个字符串复制到另外一个字符串中,包括结尾符号“\0”,会覆盖目标字符串的原始字符。

2.示例:

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

int main()
{
    char str1[] = "Hello, world!";
    char str2[20];
    strcpy(str2, str1);
    printf("The copied string is: %s\n", str2);
    return 0;
}

 3.输出结果:

The copied string is: Hello, world!

4.变体strncpy()函数:复制前n个字符

char * strncpy(char *dest, const char *src, size_t n);

三、 strcat()函数

1.作用:将一个字符串追加到另一个字符串的末尾,并且一定要保证第一个字符串有足够的空间来容纳第二个字符串。

2.示例:

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

int main()
{
    char str1[20] = "Hello, ";
    char str2[] = "world!";
    strcat(str1, str2);
    printf("The string is: %s\n", str1);
    return 0;
}

 3.输出结果:

The string is: Hello, world!

四、strcmp()函数

1.作用:比较两个字符串的大小(按照字典序一个字符一个字符对比)。它的返回值有三种情况:

       a.如果第一个字符串小于第二个字符串,则返回一个负数。

       b.如果第一个字符串等于第二个字符串,则返回0。

       c.如果第一个字符串大于第二个字符串,则返回一个正数。

2.示例:

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

int main()
{
    char str1[] = "apple";
    char str2[] = "banana";
    int result = strcmp(str1, str2);
    
    if(result < 0)
        printf("%s is less than %s\n", str1, str2);
    else if(result == 0)
        printf("%s is equal to %s\n", str1, str2);
    else
        printf("%s is greater than %s\n", str1, str2);
        
    return 0;
}

 3.输出结果:

apple is less than banana

4.变体:

        a.strncmp()函数:用于比较前n个字符

int strncmp(const char *str1, const char *str2, size_t n);

       b.strncasecmp()函数:用于比较前n个字符(忽略大小写)

int strncasecmp(const char *str1, const char *str2, size t n).

五、strspn()函数

1.作用:计算str1中连续包含str2中字符的长度。(从str1的第一个字符开始算,知道str1中出现了str2中没有的字符,停止计数并返回)

2.示例:

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

int main()
{
    char str1[] = "1234567890";
    char str2[] = "12,34567890";
    char str3[] = "123";
    size_t len1 = strspn(str1, str3);
    size_t len2 = strspn(str2, str3);
    printf("%zu\n", len1);
    printf("%zu\n", len2);
    return 0;
}

 3.输出结果:

3
2

六、strstr()函数

1.作用:判断字符串str2是否是str1的子串。如果是,则该函数返回 str1字符串从 str2第一次出现的位置开始到 str1结尾的字符串;否则,返回NULL。

2.示例:

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

int main()
{
    char str1[] = "hello, world!";
    char str2[] = "world";
    char *ptr = strstr(str1, str2);
    
    if(ptr != NULL)
        printf("The substring '%s' was found in the string '%s'\n", str2, str1);
    else
        printf("The substring '%s' was not found in the string '%s'\n", str2, str1);
        
    return 0;
}

 3.输出结果:

The substring 'world' was found in the string 'hello,world!'

4.变体strcasestr()函数:忽略大小写,其余作用一样 

七、strchr()函数

1.作用:在一个字符串中查找一个字符的位置,返回一个指向该字符串中第一次出现的字符的指针,如果字符串中不包含该字符则返回NULL空指针

2.示例:

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

int main()
{
    char str[] = "hello, world!";
    char c = 'e';
    char* ret = strchr(str,c);
    if(ret != NULL)
    {
        printf("%s\n", ret);
    }
    else
    {
        printf("char '%c' not found\n", c);
    }
    return 0;
}

 3.输出结果:

ello,world!

webserver项目源码阅读过程中出现了很多C字符串处理函数,之前接触得比较少,所以做笔记记录下来,如果觉得有用的话可以三连一手哈哈哈哈~~~

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

梦想是优秀社畜

您的打赏是对我最大的鼓励!

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

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

打赏作者

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

抵扣说明:

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

余额充值