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 )
相关推荐
xiaofeichaichai5 小时前
Webpack
前端·webpack·node.js
问心无愧05135 小时前
ctf show web入门111
android·前端·笔记
唐某人丶5 小时前
模型越来越强,我们还需要 Agent 工程吗?—— 从价值重估到 Harness 实践
前端·agent·ai编程
智码看视界5 小时前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
小欣加油5 小时前
leetcode56 合并区间
c++·算法·leetcode·职场和发展
JS菌6 小时前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
lqqjuly6 小时前
前沿算法深度解析(二)
人工智能·算法·机器学习
excel7 小时前
HLS TS 文件损坏的元凶:Git 提交与拉取
前端
Aphasia3117 小时前
https连接传输流程
前端·面试