Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code does not work ... why??

Status
Not open for further replies.

schoeppchen

IS-IT--Management
Aug 25, 2005
3
DE
Hi there,

I found a str-replace Function but it does not work ... any hints why not? It always returns the string without replacing...

-x-x-x-
#include "postgres.h"
#include <string.h>

char * pgsql_strreplace (char * string,char * search,char *replace)
{
char * cp = string;
char * s1;
char * s2;
register int z1=0;
char *tmp = malloc (strlen(string) - strlen(search) + strlen(replace)+1);

if (!*search) return string;

while (*cp)
{
s1 = cp;
s2 = search;

while ( *s1 && *s2 && !(*s1-*s2) )
s1++, s2++;

// substring found
if (!*s2) {
// Replace String attached

tmp=strcat(tmp,replace);
cp+=strlen(search);
tmp=strcat(tmp,cp);
strcpy(string,tmp);
free(tmp);
// call recursive to replace multiple entries
string=pgsql_strreplace(string,search,replace);
return string;
}

tmp[z1]=string[z1];
tmp[z1+1]=0;

cp++;
z1++;
}

free(tmp);
return string;
}

-x-x-x-
 
Here is a program that i have wrote before,i can assure you,it works perfectly !
Code:
**********************************************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *ReplaceString( char *buff, char *oldstring, char *newstring )
{
    char *bkp = (char*)malloc( sizeof(char) * strlen( buff ) * (strlen(oldstring) + 1));
    char *tmp = (char*)malloc( sizeof(char) * strlen( buff ) * (strlen(newstring) + 1));

    if( !bkp || !tmp )
    {
        fputs( &quot;memory allocation has fail.\n&quot;, stderr );
        return (NULL);
    }
    
    const int max_size = 10000;

    if( strlen( tmp ) > max_size )
    {
        fputs(&quot;not enough space in the buffer \&quot;newbuff\&quot;\n&quot;, stderr);
        return (NULL);
    }

    char newbuff[max_size];
    unsigned int len = strlen(oldstring);
    int pos;
    char *pdest;
    int i = 0;
    
    strcpy( bkp, buff );
    tmp[0] = 0;
    newbuff[0] = 0;
   
    while( buff[i] != 0 )
    {
        pdest = strstr( buff, oldstring );
        pos   = pdest - buff + 1;

        while( pdest != NULL )
        {
            bkp[pos - 1] = 0;
            strcat( bkp, newstring );
            strcat( tmp, bkp );
            buff += pos + len - 1;
            strcpy( bkp, buff );

            pdest = strstr( buff, oldstring );
            pos   = pdest - buff + 1;
        }

        i++;
    }
   
    strcat( tmp, buff );
    strcat( newbuff, tmp );

    free( bkp );
    free( tmp );
    
    
    return newbuff;
}


void main()
{
    char *String    = &quot;hi how aaa-AAAre you doing todaaa-AAAy ?&quot;;
    char *oldstring = &quot;aaa-AAA&quot;;
    char *newstring = &quot;a&quot;;
    char *temp;

    temp = ReplaceString( String, oldstring, newstring );

    if( temp != NULL )
    {
       printf(&quot;String = %s\n&quot;, String);
       printf(&quot;String = %s\n&quot;, temp );
    }
}



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top