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;
}
相关推荐
用户217516114385 分钟前
【Linux】软硬连接与动静态库
linux
小柒的博客39 分钟前
联合体union的特殊之处
c语言·机器人
奶油话梅糖1 小时前
LS-Linux-004 误删 Python 和 yum、dnf 后的恢复步骤
linux
和煦的春风1 小时前
案例分析 | SurfaceFlinger 大片Runnable引起的卡顿
android·linux
YuforiaCode2 小时前
第十二届蓝桥杯 2021 C/C++组 空间
c语言·c++·蓝桥杯
涵信2 小时前
第十二节:性能优化高频题-shallowRef/shallowReactive使用场景
linux·ubuntu·性能优化
YuforiaCode2 小时前
第十二届蓝桥杯 2021 C/C++组 卡片
c语言·c++·蓝桥杯
老秦包你会2 小时前
Linux课程五课---Linux进程认识1
linux·运维·服务器
浩浩测试一下3 小时前
网络安全实战指南:从安全巡检到权限维持的应急响应与木马查杀全(命令查收表)
linux·安全·web安全·ubuntu·网络安全·负载均衡·安全架构
我想吃余3 小时前
Linux学习笔记(一):Linux下的基本指令
linux·笔记·学习