Char *strings = "1,5,95,255"
Run Code Online (Sandbox Code Playgroud)
我想将每个数字存储到一个int变量中,然后将其打印出来.
例如,输出变为这样.
值1 = 1
value2 = 5
Value3 = 95
value4 = 255
我想在循环中执行此操作,因此如果我在字符串中有超过4个值,我应该能够获得其余的值.
我想看一个这个例子.我知道这对你们很多人来说非常基础,但我发现它有点挑战性.
谢谢
从cplusplus strtok示例修改:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char str[] ="1,2,3,4,5";
char *pt;
pt = strtok (str,",");
while (pt != NULL) {
int a = atoi(pt);
printf("%d\n", a);
pt = strtok (NULL, ",");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)