自学内容网 自学内容网

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;
}


原文地址:https://blog.csdn.net/Littlehero_121/article/details/143976518

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!