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;
}
相关推荐
派葛穆10 小时前
Lerobot -飞特舵机ST-3215双舵机联动(ESP_NOW)
c语言
一技安身10 小时前
【信创】银河麒麟V10、统信UOS解压7Z文件
linux·运维·服务器
零依赖极客11 小时前
Day 6·1 ARMv8.2 dotprod——vdotq_s32 一条指令做 4 个点积
c语言·开发语言·人工智能·矩阵·arm·vllm
奔跑的架构师11 小时前
[A-50]ARMv9/v8-DVFS系统架构
linux·arm开发·架构·系统架构·arm
迷途呀11 小时前
CI/CD 自动化部署实践:让代码提交后自动上线
linux·运维·nginx·ci/cd·自动化
赴生-11 小时前
Liunx 操作系统 进程概念(上)
linux
binqian11 小时前
【linux】OpenSSH升级文档
linux·运维·网络
吴声子夜歌12 小时前
Shell编程——if条件语句
linux·运维·shell
Dawn-bit12 小时前
Linux 运维基础扩展:跳板机、堡垒机与物理服务器全流程
linux·运维·服务器·云计算·运维开发
新时代牛马12 小时前
Linux 定时任务:crontab、anacron 与systemd timer
linux·运维·服务器