C语言中const char *字符进行切割实现

将127.0.0.1以"""."来进行切割,实现如下:

复制代码
    const char * ip = "127.0.0.1";

	char *test = new char[100];

	strcpy(test, ip);

	const char *split = ".";

	char *final;

	final = strtok(test, split);

	while (final)
	{
		printf("%s\n", final);

		final = strtok(NULL, split);
	}
	return 0;

最终实现效果:

想要将字符转成整形,如下:

复制代码
#include <stdio.h>
#include <iostream>

//借助strtok实现split
#include <string.h>

int main()
{
	const char * ip = "127.0.0.1";

	char *test = new char[100];

	strcpy(test, ip);

	const char *split = ".";

	char *final;

	final = strtok(test, split);

	while (final)
	{
		printf("%s\n", final);

		int c = atoi(final);

		printf("data = %d\n", c);

		final = strtok(NULL, split);
	}
	return 0;
}
相关推荐
mounter6258 分钟前
走向长时运行:引入协程(Coroutines),打破 BPF 程序的“一堂到底”限制
linux·ebpf·kernel
六点_dn1 小时前
Linux学习笔记-shell运算符
linux·笔记·学习
hehelm1 小时前
IO 多路复用 — Reactor
linux·服务器·网络·数据库·c++
浅止菌2 小时前
嵌入式项目Makefile越写越乱?一张万能模板+5个技巧,告别重复劳动
linux
简单电磁炮2 小时前
硬件基础4
c语言
刘某的Cloud3 小时前
手工配置nginx的systemd服务
linux·运维·网络·nginx·systemd
FREEDOM_X3 小时前
嵌入式Linux摄像头采集与图像处理
linux·图像处理·ubuntu
studytosky3 小时前
OpenClaw 入门与 Skill 开发
linux·服务器·ai编程
kaoa0003 小时前
Linux入门攻坚——82、kvm虚拟化-2
linux·运维·服务器
炸膛坦客4 小时前
单片机/C/C++八股:(二十三)#define 和 typedef 的区别
c语言·c++·单片机