This forum is in archive mode. You will not be able to post new content.

Author Topic: [C] substr - str_replace - split functions  (Read 3841 times)

0 Members and 1 Guest are viewing this topic.

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
[C] substr - str_replace - split functions
« on: 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:
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
#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;
}

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: [C] substr - str_replace - split functions
« Reply #1 on: April 04, 2011, 10:36:53 PM »
description plz and can I compile it n run on Dev-c++?

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: [C] substr - str_replace - split functions
« Reply #2 on: 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)
Satan911
Evilzone Network Administrator

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: [C] substr - str_replace - split functions
« Reply #3 on: 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

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.