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;
}
相关推荐
学逆向的3 小时前
C 循环结构的执行效率分析
c语言·开发语言·数据结构·网络安全
不昀3 小时前
网口Link Up后仍然掉线,怎样区分物理链路和数据通路问题?
linux·以太网
Lhan.zzZ4 小时前
Qt WebEngineView 在 Linux 上无法加载网页:从“看不见 HTML“到“禁用沙箱“
linux·c++·qt
Fcy6485 小时前
Linux下 多路转接select详解
linux·select·多路转接
无足鸟ICT6 小时前
【RHCA+】RPM包命名规则
linux·运维·服务器
Navigator_Z6 小时前
LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination
c语言·算法·leetcode
Zucker n6 小时前
一款适合研究生科研工作安排的日历
linux·数据库·python
爱吃香菜的初学者6 小时前
十六.Linux——多线程(2)
linux·运维·开发语言
jeo陽7 小时前
04-循环实战:素数、最大公约数、级数求和与枚举法
c语言·开发语言·数据结构