Last active
December 16, 2017 09:51
-
-
Save jnovikov/5df82935cdef61affc28df5765e3fa27 to your computer and use it in GitHub Desktop.
Strtok example with split
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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