Created
February 23, 2023 08:00
-
-
Save airvzxf/37075443a0dbdf4f28f110c48bb99811 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <cstring> | |
int main() { | |
// Convert String into Array of Strings. | |
// Managing memory is usually a feat. | |
// Furthermore, execute in terminal: `g++ main.cpp -o strings-c`. | |
const char *text = "a/apple/arm/basket/bread/car/camp/element/..."; | |
char split_char = '/'; | |
char *token, *str, *toFree; | |
// Add an empty new line and run to see how it shows different output. | |
std::cout << "Convert String into Array of Strings." << std::endl; | |
toFree = str = strdup(text); | |
while ((token = strsep(&str, &split_char))) { | |
std::cout << "Word: " << token << std::endl; | |
} | |
free(toFree); | |
free(str); | |
free(token); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment