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 )
相关推荐
a1117761 天前
医院挂号预约系统(开源 Fastapi+vue2)
前端·vue.js·python·html5·fastapi
0思必得01 天前
[Web自动化] Selenium处理iframe和frame
前端·爬虫·python·selenium·自动化·web自动化
一匹电信狗1 天前
【LeetCode_547_990】并查集的应用——省份数量 + 等式方程的可满足性
c++·算法·leetcode·职场和发展·stl
鱼跃鹰飞1 天前
Leetcode会员尊享100题:270.最接近的二叉树值
数据结构·算法·leetcode
行走的陀螺仪1 天前
uni-app + Vue3编辑页/新增页面给列表页传参
前端·vue.js·uni-app
梵刹古音1 天前
【C语言】 函数基础与定义
c语言·开发语言·算法
筵陌1 天前
算法:模拟
算法
梵刹古音1 天前
【C语言】 结构化编程与选择结构
c语言·开发语言·嵌入式
We་ct1 天前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
renhongxia11 天前
AI算法实战:逻辑回归在风控场景中的应用
人工智能·深度学习·算法·机器学习·信息可视化·语言模型·逻辑回归