EvilZone

Programming and Scripting => C - C++ => : ca0s April 04, 2011, 08:53:10 PM

: [C] substr - str_replace - split functions
: ca0s April 04, 2011, 08:53:10 PM
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;
}
: Re: [C] substr - str_replace - split functions
: gh0st April 04, 2011, 10:36:53 PM
description plz and can I compile it n run on Dev-c++?
: Re: [C] substr - str_replace - split functions
: Satan911 April 04, 2011, 10:53:11 PM
description plz and can I compile it n run on Dev-c++?

1- This is a C code. You should be able to compile it with Dev-C++ though.

2- The functions are quite clear. There's even an example if you don't get it..

3- Most of these functions already exist in C++ in the <string> library.  (Except maybe for split.. I'm not sure)
: Re: [C] substr - str_replace - split functions
: gh0st April 05, 2011, 03:30:59 PM
dude Idk how you read it but I just can view a bit of code desordered  :P Ive tried to run it on Dev C++ however atm Im learning C++ I will focus on C in a nearly future