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;
}
相关推荐
侯小啾2 小时前
【03】C语言 强制类型转换 与 进制转换
c语言·数据结构·算法
Sylvia@8882 小时前
18.SELInux安全性
linux·运维·服务器
Xの哲學2 小时前
Linux NAPI 架构详解
linux·网络·算法·架构·边缘计算
碼不停提2 小时前
linux 内核架构
linux
云知谷3 小时前
【经典书籍】C++ Primer 第15章类虚函数与多态 “友元、异常和其他高级特性” 精华讲解
c语言·开发语言·c++·软件工程·团队开发
gplitems1234 小时前
Technox – IT Solutions & Services WordPress Theme: A Practical
linux·服务器·数据库
Deamon Tree4 小时前
后端开发常用Linux命令
linux·运维·python
key_Go5 小时前
0.基于Centos-Stream8 搭建Openstack环境
linux·运维·centos·openstack
wacpguo5 小时前
centos 配置网络
linux·网络·centos
feng_blog66886 小时前
Linux桌面X11服务-XRecord方案捕获鼠标点击的应用窗口
linux