Skip to content

Instantly share code, notes, and snippets.

@jnovikov
Last active December 16, 2017 09:51
Show Gist options
  • Save jnovikov/5df82935cdef61affc28df5765e3fa27 to your computer and use it in GitHub Desktop.
Save jnovikov/5df82935cdef61affc28df5765e3fa27 to your computer and use it in GitHub Desktop.
Strtok example with split
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAX_LEN = 101;
int split_words(char *text,char *div,char out[][MAX_LEN]) {
int counter = 0;
char *ptr = strtok(text,div);
while (ptr != NULL) {
strncpy(out[counter],ptr,strlen(ptr)); // В массив вывода добавили токен
counter++;
ptr = strtok(NULL,div);
}
delete []ptr;
return counter;
}
int main() {
char text[40] = "Hello, this! beautiful_world \n";
char div[] = " ,!_\n";
char out[100][MAX_LEN] = {};
int words_count = split_words(text,div,out);
for (int i = 0; i < words_count; i++) {
cout << out[i] << " ";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment