Skip to content

Instantly share code, notes, and snippets.

@LessUp
Created June 16, 2022 07:15
Show Gist options
  • Select an option

  • Save LessUp/23072b5984fe5baee40e30b5814901f0 to your computer and use it in GitHub Desktop.

Select an option

Save LessUp/23072b5984fe5baee40e30b5814901f0 to your computer and use it in GitHub Desktop.
[字符串分割]不改变原字符串 #strtok #split
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
const char *strtok_p(const char *p, const char delim, const char **pos) {
// 分割结束
if (*p == '\0') {
return nullptr;
}
const char *cur = p;
int cnt = 0;
while (*cur != '\0' && *cur != delim) {
cur++;
cnt++;
}
// 指向下一个字符串开头
if (*cur != '\0') {
cur++;
}
*pos = cur;
long len = cur - p;
char *res = new char[len];
memcpy(res, p, len);
res[len - 1] = '\0';
return res;
}
int main() {
setvbuf(stdout, NULL, _IONBF, 0);
const char *s = "Geeks\nfor\nGeeks";
const char **pos = &s;
while (const char *line = strtok_p(s, '\n', pos)) {
printf("%s\n", line);
delete[] line;
s = *pos;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment