c语言学习笔记2

字符串函数

字符串函数包含在 string.h 这个头文件中

  • strlen():返回不包括字符串结尾’\0’的字符串长度,即有效字符长度
    int strlen(const char* str);  // 函数原型
    int a = strlen(str);  //函数使用案例
  • strnlen():返回len个char长度以内,不包括字符串结尾’\0’的字符串长度
    int strnlen(const char* str, int maxlen);  //函数原型
    int a = strlen(str, len);  //函数使用案例
  • strcat():将参数str2追加到str1里
    int strcat(char* str1, const char* str2);  // 函数原型
    strcat(str1, str2);  // 函数使用案例
  • strncat():字符串有限追加len个char字节长度的字符串
    int strncat(str1, str2, len);  // 函数原型
    strncat(str1, str2, len);  //函数使用案例
  • strtok():分解字符串str为一组字符串子串,石头人就是要被分隔的支付穿,delim为分隔符
    char* strtok(char* str, const char* str2, int len);  // 函数原型
    strtok(str, delim);  // 函数案例

strtok()函数每次会把分隔符坐在的位置置为’\0’,调用前和调用后的str已经不一样了

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

int main(void)
{
    char s[20] = "abcsabscccs";

    strtok(s, "s");
    printf("%s\n", s);

    strtok(NULL, "s");
    printf("%s\n", s);

    strtok(NULL, "s");
    printf("%s\n", s);
}

案例输出结果为

    abc
    abc
    abc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值