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;
}
相关推荐
微露清风44 分钟前
C语言习题讲解-第九讲- 常见错误分类等
c语言·开发语言
j_xxx404_1 小时前
数据结构:算法复杂度与空间复杂度
c语言·数据结构·算法
AI视觉网奇1 小时前
whisper tokenizer
linux·运维·服务器
NotStrandedYet2 小时前
信创国产Linux操作系统汇总:从桌面到服务器,百花齐放
linux·信创·国产化
特种加菲猫2 小时前
从文件到文件描述符:理解程序与文件的交互本质
linux·笔记
懋学的前端攻城狮3 小时前
深入浅出Linux-01:系统化掌握基础操作
linux·后端
好好先森&3 小时前
C语言:冒泡排序
c语言·数据结构·算法·遍历·冒牌排序
孙克旭_3 小时前
day064-kodbox接入对象存储与配置负载均衡
linux·运维·阿里云·负载均衡
李永奉4 小时前
C语言-字符串(定义)、字符串函数(strlen、strcat、strcpy、strcmp、strlwr、strupr)
c语言·开发语言·算法
大锦终4 小时前
【Linux】环境变量
linux·运维·服务器