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;
}
相关推荐
为思念酝酿的痛7 小时前
POSIX信号量
linux·运维·服务器·后端
人还是要有梦想的8 小时前
linux下用搜狗输入法,中英文切换
linux·运维·服务器
bush48 小时前
嵌入式linux学习记录二
linux·运维·学习
9分钟带帽8 小时前
linux_通过NFS挂载远程服务器的硬盘
linux·服务器
Bluetooth7309 小时前
c语言一维数组
c语言
QiLinkOS10 小时前
【从实验室到商业战场:发明专利如何重塑科技与企业的共生生态】
大数据·c语言·数据结构·c++·人工智能·单片机·算法
运维栈记11 小时前
API Error: 400 Request body format invalid
linux·ai
小白兔奶糖ovo11 小时前
【Leetcode】231. 2的幂
linux·算法·leetcode
s_w.h12 小时前
【 linux 】动静态库的制作
linux·运维·服务器·算法·bash
顺风尿一寸12 小时前
深入Linux内核:mkdir系统调用的完整实现解析
linux