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 )
相关推荐
Nil2081 小时前
leetcode 118杨辉三角
算法·leetcode·职场和发展
用户033307413953 小时前
TypeScript using 与 AsyncDisposableStack:在真实服务端代码中协调多资源清理
前端
daisychey3 小时前
用 Playwright 做多平台内容分发,我踩过的 8 个坑
前端
wanderist.3 小时前
线性筛法详解:从筛质数到欧拉函数、Möbius 函数与约数函数
java·数据结构·算法
All for pursuit.4 小时前
【设计-1】208.实现Trie (前缀树)
数据结构·c++·算法·leetcode
weixin_307779134 小时前
C++代码实现MATLAB中的crossval函数功能
开发语言·c++·算法·matlab
青少儿编程课堂4 小时前
背包问题(0/1 背包与完全背包)解题精讲——动态规划入门
c++·python·算法·bfs·信息学竞赛
IT_陈寒4 小时前
为什么你应该学习JavaScript?
前端·人工智能·后端
大龄秃头程序员4 小时前
iOS冷启动监控Demo
前端
光影少年4 小时前
为什么 JavaScript 中 0.1 + 0.2 !== 0.3,如何让其相等?
前端·javascript·算法