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;
}
相关推荐
CYRUS_STUDIO14 小时前
用 Frida 控制 Android 线程:kill 命令、挂起与恢复全解析
android·linux·逆向
熊猫李15 小时前
rootfs-根文件系统详解
linux
dessler18 小时前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
泽泽爱旅行18 小时前
awk 语法解析-前端学习
linux·前端
轻松Ai享生活2 天前
5 节课深入学习Linux Cgroups
linux
christine-rr2 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神5552 天前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆2 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
乌萨奇也要立志学C++2 天前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
小莞尔2 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机