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 小时前
Linux 上使用 CLion 开发嵌入式,并用 Codex CLI
linux·运维·服务器
张3231 小时前
Ansible拆分大型Playbook
linux·ansible
dgaf2 小时前
谢谢 AI (打靶测试用文)
c语言·gpt·ai编程·d3d12
苦逼大学生被编程薄纱2 小时前
Ext 文件系统基础:Linux 存储基石入门(下)
linux·运维·服务器
Lumos_7772 小时前
Linux -- 进程
linux·运维·服务器
南境十里·墨染春水3 小时前
linux学习进展 进程间通讯——共享内存
linux·数据库·学习
一行代码一行诗++3 小时前
C语言中scanf详解
c语言·开发语言
小此方3 小时前
Re:Linux系统篇(五)指令篇 ·四:shell外壳程序及其工作原理
linux·运维·服务器
ZenosDoron3 小时前
keil软件修改字体,Asm editor,和C/C++ editor的区别
c语言·开发语言·c++
其实防守也摸鱼3 小时前
sqlmap下载和安装保姆级教程(附安装包)
linux·运维·服务器·测试工具·渗透测试·攻防·护网行动