[英]how to only get the terms in the bracket using strtok
我的输入是(2,3,4),(2,5,4)
我只想得到2 3 4 \n 2 5 4
作为我的 output。 谁能帮我这个?
#include<stdio.h>
#include <string.h>
int main() {
char string[50] = "(2,3,4),(2,5,4)";
// Extract the first token
char * token = strtok(string, "()");
// loop through the string to extract all other tokens
while( token != NULL ) {
printf( " %s\n", token ); //printing each token
token = strtok(NULL, "()");
}
return 0;
}
``
我已经使用标准strtok()
而不是评论中提到的非标准strsep()
实现了这一点。
#include <stdio.h>
#include <string.h>
int main(void) {
char input[] = "(2,3,4),(2,5,4)";
char *tok = strtok(input, "( ,)" );
for(int i = 0; i < 2; i++) {
// each set
for(int j = 0; j < 3; j++) {
// break each set
if(tok) {
printf("%s ", tok); //printing each token
tok = strtok(NULL, "( ,)" );
}
}
printf("\n");
}
return 0;
}
程序 output
2 3 4
2 5 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.