本文介绍了数组和strpbrk用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我的数组是:
char* String_Buffer = "Hi my name is <&1> and i have <&2> years old."
char* pos = strpbrk(String_buffer, "<");
现在pos是:
&LT;&功放; 1>和我有&LT;&安培; 2>岁的
但我需要我的名字是。如何才能做到这一点?
But i need "Hi my name is". How can do this?
推荐答案
如果您跟踪启动
分开,可以通过切出缓冲区的部分:
If you track start
separately, you can "cut out" a section of the buffer:
char *start = String_Buffer;
char *end = strpbrk(String_Buffer, "<");
if (end) {
/* found it, allocate enough space for it and NUL */
char *match = malloc(end - start + 1);
/* copy and NUL terminate */
strncpy(match, start, end - start);
match[end - start] = '\0';
printf("Previous tokens: %s\n", match);
free(match);
} else {
/* no match */
}
要走路缓冲打印每个令牌,你只需吊成一个圈这样的:
To walk the buffer printing each token, you'll simply hoist this into a loop:
char *start = String_Buffer, *end, *match;
while (start) {
end = strpbrk(start, "<");
if (!end) {
printf("Last tokens: %s\n", start);
break;
} else if (end - start) {
match = malloc(end - start + 1);
/* copy and NUL terminate */
strncpy(match, start, end - start);
match[end - start] = '\0';
printf("Tokens: %s\n", match);
free(match);
end++; /* walk past < */
}
/* Walk to > */
start = strpbrk(end, ">");
if (start) {
match = malloc(start - end + 1); /* start > end */
strncpy(match, end, start - end);
match[start - end] = '\0';
printf("Bracketed expression: %s\n", match);
free(match);
start++; /* walk past > */
}
}
这篇关于数组和strpbrk用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!