SkillAgentSearch skills...

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.c
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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

View on GitHub
GitHub Stars42
CategoryDevelopment
Updated1y ago
Forks10

Languages

C

Security Score

75/100

Audited on Sep 9, 2024

No findings