Skip to content

Instantly share code, notes, and snippets.

@airvzxf
Created February 23, 2023 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airvzxf/37075443a0dbdf4f28f110c48bb99811 to your computer and use it in GitHub Desktop.
Save airvzxf/37075443a0dbdf4f28f110c48bb99811 to your computer and use it in GitHub Desktop.
#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