I coded them short time ago. The may have bugs, I have not tested them a lot.
substr:
char *substr(char *str, int begin, int len)
{
int strLen=strlen(str);
if(strLen<begin) return str;
if((len>strLen) || (len==0)) len=strLen;
if((strLen-begin)<len) len=strLen-begin;
str+=begin;
char *ret=(char *)malloc(len+1);
memset(ret, 0, len+1);
strncpy(ret, str, len);
return ret;
}
str_replace:
char *str_replace(char *str, char *what, char *with)
{
int strLen=strlen(str);
int whatLen=strlen(what);
int withLen=strlen(with);
signed int delta=0;
delta=withLen-whatLen;
int n=0;
char *foo=strstr(str, what);
if(foo==NULL) return str;
while(foo!=NULL)
{
foo=strstr(foo+whatLen, what);
n++;
}
int newLen=strLen+(n*delta);
char *res=(char *)malloc(newLen+1);
memset(res, 0, newLen+1);
foo=strstr(str, what);
while(foo!=NULL)
{
strncat(res, str, foo-str);
strcat(res, with);
str=foo+whatLen;
foo=strstr(str, what);
}
strcat(res, str);
return res;
}
split:
char **split(char *str, char *tok)
{
// how many parts?
int strLen=strlen(str);
char *foo=strstr(str, tok);
if(foo==NULL) return NULL;
int n=0;
while(foo!=NULL)
{
foo=strstr(foo+strlen(tok), tok);
n++;
}
char **res=(char **)malloc((n+1)*sizeof(char *));
// First part
foo=strstr(str, tok);
*res=(char *)malloc(foo-str+1);
memset(*res, 0, foo-str+1);
strncpy(*res, str, foo-str);
int i=1;
// Middle parts
str=foo+strlen(tok);
for(i=1; i<n; i++)
{
foo=strstr(str, tok);
*(res+i)=(char *)malloc(foo-str+1);
memset(*(res+i), 0, foo-str+1);
strncpy(*(res+i), str, foo-str);
str=foo+strlen(tok);
}
// Last part
i=0;
while(*(str+i)!='\0') i++;
*(res+n)=(char *)malloc(i+1);
memset(*(res+n), 0, i+1);
strncpy(*(res+n), str, i);
// Lets run
return res;
}
Example:
#include <stdio.h>
#include <ca0sStrFuncs.c>
int main()
{
char test[]="This is only a test";
printf("Test substr: %s\n", substr(test, 15, 0);
printf("Test str_replace: %s\n", str_replace(str, "only", "just");
char **res=split(test, " ");
int i=0;
while(res[i]!=NULL)
{
printf("%s\n", res[i]);
i++;
}
return 0;
}