简体   繁体   English

使用strtok()C分割char数组

[英]split a char array using strtok() C

I have a char array called temps that is passed to a function. 我有一个名为temps的char数组,该数组传递给函数。 The format will always be like this: 格式将始终是这样的:

1 1.1 1 1.1

I want to split it up and save these two numbers. 我想将其拆分并保存这两个数字。 These is a space between them but after researching strtok() , I have no idea how it works. 它们之间是一个空格,但是在研究strtok() ,我不知道它是如何工作的。

void seperate(char *tempformat){
    char s1[10];
    char s2[10];

    s1 = strtok();
    s2 = strtok();
}

You can do it as : 您可以按照以下方式进行操作:

char *result;
char s=" "; //space(delimiter)
char str[];
result = strtok(tempformat, s); 
void seperate(char *tempformat){
   while( result != NULL ) {
      printf( " %s\n", result ); //You can save the result into a array of characters as well.
      result = strtok(NULL, s);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM