C--字符串函数处理总结

文章目录

      • 函数接口
      • 常见应用
        • [int 转化 字符串](#int 转化 字符串)
          • [1 2 3 4 (int ) ---> 1,2,3,4 (char [])](#1 2 3 4 (int ) ---> 1,2,3,4 (char []))
        • [字符串转化为 int](#字符串转化为 int)
          • [1,2,3,4(char []) ---> 1 2 3 4 (int )](#1,2,3,4(char []) ---> 1 2 3 4 (int ))

函数接口

strchr

char *strchr(const char *str, int c)

在参数 str 所指向的字符串中搜索第一次出现字符 c

返回值:

该函数返回在字符串 str 中第一次出现字符 c 的位置,如果未找到该字符则返回 NULL

c 复制代码
#include <stdio.h>
#include <string.h>

int main ()
{
   const char str[] = "helloworld";
   const char ch = 'w';
   char *ret;

   ret = strchr(str, ch);

   printf("%s", ret);
   
   return(0);
}

输出:world

strtok

char *strtok(char *str, const char *delim);

str: 要分词的字符串。在首次调用时,此参数应指向欲分解的字符串。在后续的调用中,为了获取其他的标记,这个参数应当为 NULL。

delim: 分隔符

返回值:

如果找到一个标记,则返回指向该标记的指针。

如果没有找到标记或已经达到字符串的末尾,则返回 NULL。

c 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char string[50] = "Hello,world,apple";
    char *token = strtok(string, ","); // 使用逗号作为分隔符

    while(token != NULL) {
        printf("%s\n", token);
        token = strtok(NULL, ",");
    }

    return 0;
}

输出:

Hello

world

apple

常见应用

int 转化 字符串
1 2 3 4 (int ) ---> 1,2,3,4 (char [])
字符串转化为 int
1,2,3,4(char []) ---> 1 2 3 4 (int )
相关推荐
一点一木13 小时前
深度体验TRAE SOLO移动端7天:作为独立开发者,我把工作流揣进了兜里
前端·人工智能·trae
天外飞雨道沧桑14 小时前
TypeScript 中 omit 和 record 用法
前端·javascript·typescript
Lee川14 小时前
mini-cursor 揭秘:从 Tool 定义到 Agent 循环的完整实现
前端·人工智能·后端
Dlrb121114 小时前
C语言-指针三
c语言·算法·指针·const·命令行参数
kkeeper~14 小时前
0基础C语言积跬步之深入理解指针(5下)
c语言·开发语言
Tisfy14 小时前
LeetCode 2540.最小公共值:双指针(O(m+n))
算法·leetcode·题解·双指针
IronMurphy14 小时前
【算法四十七】152. 乘积最大子数组
算法
canonical_entropy15 小时前
从 Spec-Driven Development 到 Attractor-Guided Engineering
前端·aigc·ai编程
研☆香15 小时前
聊聊前端页面的三种长度单位
前端
给钱,谢谢!15 小时前
React + PixiJS 实现果园成长页:从状态机到浇水动画
前端·react.js·前端框架