Strsplit.c
Split/cut a string into an array with a string delimiter in C. The function is written to have only one malloc/free per call 🔥
Install / Use
/learn @mr21/Strsplit.cREADME
strsplit.c
strsplit() cut a string into an array with a string delimiter.
The function is written to have only one malloc / free per call.
The array can be reordered or shrinked but its length can not be increased due to the original string which has been copied right after the last NULL pointer.
Prototypes
char** strsplit( const char* s, const char* del );
char** strsplit_count( const char* s, const char* del, size_t* nb );
Examples
#include <stdio.h>
#include <stdlib.h>
#include "strsplit.h"
int main( void ) {
char** arr = strsplit( "qwerty:asdfgh::zxcvbn", ":" );
if ( arr ) {
printf( "%s\n", arr[ 0 ] ); // "qwerty"
printf( "%s\n", arr[ 1 ] ); // "asdfgh"
printf( "%s\n", arr[ 2 ] ); // ""
printf( "%s\n", arr[ 3 ] ); // "zxcvbn"
printf( "%p\n", arr[ 4 ] ); // NULL
free( arr );
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include "strsplit.h"
int main( void ) {
size_t nb;
char** arr = strsplit_count( "qwerty:asdfgh::zxcvbn", ":", &nb );
if ( nb == 4 ) {
printf( "%s\n", arr[ 0 ] ); // "qwerty"
printf( "%s\n", arr[ 1 ] ); // "asdfgh"
printf( "%s\n", arr[ 2 ] ); // ""
printf( "%s\n", arr[ 3 ] ); // "zxcvbn"
printf( "%p\n", arr[ 4 ] ); // NULL
free( arr );
}
return 0;
}
Related Skills
node-connect
349.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
