string search and replace

Discussion in 'C Programming' started by int main(void), Oct 11, 2006.

  1. Hi all,

    Following is my attempt to write a string search and replace
    function.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*-------------------------------------------------------------------------------------------
    | Function name : strrep
    |
    | Arguments : src,find,rep
    |
    | src string should be *big* enough to hold the result
    |
    | Return Value : Number of replacements made |
    --------------------------------------------------------------------------------------------*/
    int strrep(char *src,const char *find,const char *rep)
    {
    char *remaining; /* pointer to the left over part
    */
    int count = 0; /* no. of replacements
    */
    size_t find_len = strlen(find); /* length of find string
    */
    size_t rep_len = strlen(rep); /* length of replace string */

    if(find_len == 0) return 0; /* nothing to find */

    /*Initially allocate memory to store whole of src */
    if( (remaining = malloc(strlen(src) + 1)) == NULL) /*remember to
    free this*/
    {
    printf("Sorry out of memory !");
    return 0;
    }
    while( (src = strstr(src,find)) != NULL ) /* get the matching
    position */
    {
    count++; /*Count the num of
    replacements*/
    strcpy(remaining,src + find_len); /*store left over string
    */
    strcpy(src,rep); /*make the
    replacement */
    src += rep_len; /*move to end of
    replacement */
    strcpy(src,remaining); /*copy back the left
    over part */
    }

    free(remaining);
    return count;
    }


    I know that this code is inefficient (it uses all the string
    library routines),
    I wanted to know how to improve this code. Can anyone help me ?
    I want to know how you would implement the same function
    I know that there are some Regex libraries to do this effectively.
    But why is such a common function not there in standard library ?


    Thanks for your time,
    Yugi
     
    int main(void), Oct 11, 2006
    #1
    1. Advertising

  2. int main(void) wrote:

    > Hi all,
    >
    > Following is my attempt to write a string search and replace
    > function.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    > /*-------------------------------------------------------------------------------------------
    > | Function name : strrep
    > |
    > | Arguments : src,find,rep
    > |
    > | src string should be *big* enough to hold the result
    > |
    > | Return Value : Number of replacements made |
    > --------------------------------------------------------------------------------------------*/
    > int strrep(char *src,const char *find,const char *rep)
    > {
    > char *remaining; /* pointer to the left over part
    > */
    > int count = 0; /* no. of replacements
    > */
    > size_t find_len = strlen(find); /* length of find string
    > */
    > size_t rep_len = strlen(rep); /* length of replace string */
    >
    > if(find_len == 0) return 0; /* nothing to find */
    >
    > /*Initially allocate memory to store whole of src */
    > if( (remaining = malloc(strlen(src) + 1)) == NULL) /*remember to
    > free this*/
    > {
    > printf("Sorry out of memory !");
    > return 0;
    > }
    > while( (src = strstr(src,find)) != NULL ) /* get the matching
    > position */
    > {
    > count++; /*Count the num of
    > replacements*/
    > strcpy(remaining,src + find_len); /*store left over string
    > */
    > strcpy(src,rep); /*make the
    > replacement */
    > src += rep_len; /*move to end of
    > replacement */
    > strcpy(src,remaining); /*copy back the left
    > over part */
    > }
    >
    > free(remaining);
    > return count;
    > }
    >
    >
    > I know that this code is inefficient (it uses all the string
    > library routines),
    > I wanted to know how to improve this code. Can anyone help me ?
    > I want to know how you would implement the same function
    > I know that there are some Regex libraries to do this effectively.
    > But why is such a common function not there in standard library ?
    >
    >
    > Thanks for your time,
    > Yugi



    Sorry for that bad layout. This would be good.

    Hi all,


    Following is my attempt to write a string search and replace
    function.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*---------------------------------------------------------------------------
    | Function name : strrep
    | Arguments : src,find,rep
    | src string should be *big* enough to hold the result
    | Return Value : Number of replacements made
    ----------------------------------------------------------------------------*/
    int strrep(char *src,const char *find,const char *rep)
    {
    char *remaining; /*pointer to the left over part */
    int count = 0; /* no.of replacements*/
    size_t find_len = strlen(find);/*length of find string*/
    size_t rep_len = strlen(rep);/*length of replace string*/

    if(find_len == 0) return 0; /* nothing to find */

    /*Initially allocate memory to store whole of src */
    if( (remaining = malloc(strlen(src) + 1)) == NULL)
    {
    printf("Sorry out of memory !");
    return 0;
    }
    /* get the matching position*/
    while( (src = strstr(src,find)) != NULL )
    {
    count++; /*Count the num of replacements*/
    strcpy(remaining,src + find_len); /*store left over string */
    strcpy(src,rep); /*make the replacement*/
    src += rep_len; /*move to end of replacement*/
    strcpy(src,remaining); /*copy back the left over part*/
    }

    free(remaining);
    return count;
    }


    I know that this code is inefficient (it uses all the string
    library routines),
    I wanted to know how to improve this code. Can anyone help me ?
    I want to know how you would implement the same function
    I know that there are some Regex libraries to do this effectively.
    But why is such a common function not there in standard library ?


    Thanks for your time,
    Yugi
     
    int main(void), Oct 11, 2006
    #2
    1. Advertising

  3. int main(void) wrote:

    > Hi all,
    >
    > Following is my attempt to write a string search and replace
    > function.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    > /*-------------------------------------------------------------------------------------------
    > | Function name : strrep
    > |
    > | Arguments : src,find,rep
    > |
    > | src string should be *big* enough to hold the result
    > |
    > | Return Value : Number of replacements made |
    > --------------------------------------------------------------------------------------------*/
    > int strrep(char *src,const char *find,const char *rep)
    > {
    > char *remaining; /* pointer to the left over part
    > */
    > int count = 0; /* no. of replacements
    > */
    > size_t find_len = strlen(find); /* length of find string
    > */
    > size_t rep_len = strlen(rep); /* length of replace string */
    >
    > if(find_len == 0) return 0; /* nothing to find */
    >
    > /*Initially allocate memory to store whole of src */
    > if( (remaining = malloc(strlen(src) + 1)) == NULL) /*remember to
    > free this*/
    > {
    > printf("Sorry out of memory !");
    > return 0;
    > }
    > while( (src = strstr(src,find)) != NULL ) /* get the matching
    > position */
    > {
    > count++; /*Count the num of
    > replacements*/
    > strcpy(remaining,src + find_len); /*store left over string
    > */
    > strcpy(src,rep); /*make the
    > replacement */
    > src += rep_len; /*move to end of
    > replacement */
    > strcpy(src,remaining); /*copy back the left
    > over part */
    > }
    >
    > free(remaining);
    > return count;
    > }
    >
    >
    > I know that this code is inefficient (it uses all the string
    > library routines),
    > I wanted to know how to improve this code. Can anyone help me ?
    > I want to know how you would implement the same function
    > I know that there are some Regex libraries to do this effectively.
    > But why is such a common function not there in standard library ?
    >
    >
    > Thanks for your time,
    > Yugi



    Sorry for that bad layout. This would be good.

    Hi all,


    Following is my attempt to write a string search and replace
    function.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*---------------------------------------------------------------------------
    | Function name : strrep
    | Arguments : src,find,rep
    | src string should be *big* enough to hold the result
    | Return Value : Number of replacements made
    ----------------------------------------------------------------------------*/
    int strrep(char *src,const char *find,const char *rep)
    {
    char *remaining; /*pointer to the left over part */
    int count = 0; /* no.of replacements*/
    size_t find_len = strlen(find);/*length of find string*/
    size_t rep_len = strlen(rep);/*length of replace string*/

    if(find_len == 0) return 0; /* nothing to find */

    /*Initially allocate memory to store whole of src */
    if( (remaining = malloc(strlen(src) + 1)) == NULL)
    {
    printf("Sorry out of memory !");
    return 0;
    }
    /* get the matching position*/
    while( (src = strstr(src,find)) != NULL )
    {
    count++; /*Count the num of replacements*/
    strcpy(remaining,src + find_len); /*store left over string */
    strcpy(src,rep); /*make the replacement*/
    src += rep_len; /*move to end of replacement*/
    strcpy(src,remaining); /*copy back the left over part*/
    }

    free(remaining);
    return count;
    }


    I know that this code is inefficient (it uses all the string
    library routines),
    I wanted to know how to improve this code. Can anyone help me ?
    I want to know how you would implement the same function
    I know that there are some Regex libraries to do this effectively.
    But why is such a common function not there in standard library ?


    Thanks for your time,
    Yugi
     
    int main(void), Oct 11, 2006
    #3
  4. int main(void)

    Chris Dollin Guest

    int main(void) wrote:

    Sending the same message three times does not endear you to us.
    Sending the same message thrre times does not endear you to us.
    Sending the same message three times does not endeer you to us.

    > But why is such a common function not there in standard library ?


    Likely:

    Because search-and-replace may need to expand the target string,
    and there isn't an obvious single best way to do this.

    (And because there wasn't one commonly implemented when the first
    standard was produced.)

    --
    Chris "Essen -8 and counting" Dollin
    "A facility for quotation covers the absence of original thought." /Gaudy Night/
     
    Chris Dollin, Oct 11, 2006
    #4
  5. Chris Dollin wrote:

    > int main(void) wrote:
    >
    > Sending the same message three times does not endear you to us.
    > Sending the same message thrre times does not endear you to us.
    > Sending the same message three times does not endeer you to us.


    My apologies. I sent the second time because layout of the code
    after
    posting got srambled somehow.
    I dont know why it got posted the third time.
    I'm using google interface.
    >
    > > But why is such a common function not there in standard library ?

    >
    > Likely:
    >
    > Because search-and-replace may need to expand the target string,
    > and there isn't an obvious single best way to do this.
    >
    > (And because there wasn't one commonly implemented when the first
    > standard was produced.)

    Thanks for that point.


    Thanks for your time,
    Yugi.
     
    int main(void), Oct 11, 2006
    #5
  6. int main(void)

    CBFalconer Guest

    "int main(void)" wrote:
    > Chris Dollin wrote:
    >> int main(void) wrote:
    >>

    .... snip about string search and replace ...
    >>
    >>> But why is such a common function not there in standard library ?

    >>
    >> Likely:
    >>
    >> Because search-and-replace may need to expand the target string,
    >> and there isn't an obvious single best way to do this.
    >>
    >> (And because there wasn't one commonly implemented when the first
    >> standard was produced.)

    >
    > Thanks for that point.


    And because the function is rarely needed. It may be useful in
    replacing strings during a file copy operation, which is a
    different matter. It might be instructive to examine how
    identifiers are replaced in id2id, for which see id2id-20.zip at:

    <http://cbfalconer.home.att.net/download/>

    --
    Some informative links:
    <news:news.announce.newusers
    <http://www.geocities.com/nnqweb/>
    <http://www.catb.org/~esr/faqs/smart-questions.html>
    <http://www.caliburn.nl/topposting.html>
    <http://www.netmeister.org/news/learn2quote.html>
    <http://cfaj.freeshell.org/google/>
     
    CBFalconer, Oct 11, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Ram

    string-search-and-replace function wanted

    Stefan Ram, Mar 4, 2004, in forum: C Programming
    Replies:
    11
    Views:
    827
    anony*mouse
    Mar 4, 2004
  2. Alun
    Replies:
    3
    Views:
    4,519
    Masudur
    Feb 18, 2008
  3. Abby Lee
    Replies:
    5
    Views:
    419
    Abby Lee
    Aug 2, 2004
  4. Prasad S
    Replies:
    2
    Views:
    235
    Dr John Stockton
    Aug 27, 2004
  5. V S Rawat
    Replies:
    5
    Views:
    308
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page