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;
}
相关推荐
无奈笑天下1 小时前
银河麒麟桌面OS使用分区编辑器将/backup分区删除并扩容至根分区参考教程
linux·数据库·经验分享·编辑器
CheungChunChiu7 小时前
Linux 内核设备模型与驱动框架解析 ——以 rk-pcie 为例
linux·运维·ubuntu
列逍8 小时前
Linux进程(三)
linux·运维·服务器·环境变量·命令行参数
水天需0108 小时前
VS Code Ctrl+Shift+V 预览 Markdown 无效的解决方案
linux
吃西瓜的年年9 小时前
1. 初识C语言
c语言·开发语言
永远都不秃头的程序员(互关)10 小时前
C语言 基本语法
c语言·开发语言
赖small强11 小时前
【Linux C/C++开发】Linux 平台 Stack Protector 机制深度解析
linux·c语言·c++·stack protector·stack-protector·金丝雀机制
陌路2012 小时前
Linux42 守护进程
linux
liteblue12 小时前
DEB包解包与打包笔记
linux·笔记
liu****12 小时前
3.链表讲解
c语言·开发语言·数据结构·算法·链表