小编Mar*_*kus的帖子

C 中将字符从字符串复制到另一个字符串

我有一个字符串 AAbbCC 我需要的是复制前两个并将它们添加到数组中,然后复制中间两个并将它们添加到数组中,最后复制最后两个并将它们添加到数组中。

这就是我所做的:

char color1[2];
char color2[2];
char color3[2];

strncpy(color1, string, 2); // I take the first two characters and put them into color1

// now I truncate the string to remove those AA values:

string = strtok(string, &color1[1]);

// and when using the same code again the result in color2 is bbAA:

strncpy(color2, string, 2); 
Run Code Online (Sandbox Code Playgroud)

它通过了那些 bb,但也通过了前一个的 AA ..即使数组只有两个位置,当我在其上使用 strtol 时,它给了我一些大值,而不是我正在寻找的 187 ..如何摆脱它?或者如何让它以其他方式工作?任何意见,将不胜感激。

c string truncate char strncpy

3
推荐指数
1
解决办法
3万
查看次数

标签 统计

c ×1

char ×1

string ×1

strncpy ×1

truncate ×1