Results 1 to 6 of 6

Thread: Replacing bits of strings

  1. #1
    conquerdude
    Guest

    Replacing bits of strings

    Ok...what's the VB equivalent of Replace()? I have 2 variables, mainstring and username. Let's say mainstring contains :

    $user is cool!

    If I wanted to make $user to what's in username I would do

    mainstring = Replace(mainstring, "$user", username)

    Now how would I do this in C++??

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    conquerdude
    Guest
    Well i guess i shouldn't be saying string but character array. It's not a string.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I am responsible for that function, I've tested it with replacing longer replace strings as well so I assumed it would work assuming there is enough target buffer.
    With what input did it fail?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    conquerdude
    Guest
    This is the exact bit of code I'm using:
    PHP Code:
    char tmp[211];
    int maxwidthofline 210;     // put the max line len here
    FILE *in;
    in=fopen("responses.txt","r");
    char seps[] = ":";
    char *token;

    char playername[80];

    int didwe 0;
    int firstnum;
    char *whatsaid;
    char *saydis;
    while (!
    feof(in) )
    {

        
    fgets(tmp,maxwidthofline,in);
        
    strcpy(playernameSTRING(pEntity->v.netname));
        
    replacestr(tmp"$player"playernametmp);
        
    // Establish string and get the first token:
        
    token strtoktmpseps );
        
    firstnum atoi(token);
        
    //DOES SOME STUFF
    }
    fclose(in); 
    This is part of the code I'm coding for AdminOP (a TFC server side mod). The STRING(pEntity->v.netname) justs returns the player name. So anyways, what is wrong with this?

  6. #6
    conquerdude
    Guest
    Well here's some more examples:

    This works:
    PHP Code:
    #include <string.h> 
    #include <stdio.h> 

    charreplacestr(const charstr,const charcrt,const charrep,char*trg){
        
    chartemp=trg;
        for(;*
    str; ){
            for(const 
    char*i=crt,*j=str;*j&&*i&&*i==*j;j++,i++);
            if (!*
    i){
                for(
    i=rep;*i;*trg++=*i++)str++;
                
    str=j;
            }else 
                *
    trg++=*str++;
        }
        *
    trg=0;
        return 
    temp;

    };

    void mainvoid 
    {
        
    char tmp[256] = "$user is cool\n";
        
    char username[80]="Me";
        
    replacestr(tmp"$user"usernametmp);
        
    printf(tmp);

    This doesn't
    PHP Code:
    #include <string.h> 
    #include <stdio.h> 

    charreplacestr(const charstr,const charcrt,const charrep,char*trg){
        
    chartemp=trg;
        for(;*
    str; ){
            for(const 
    char*i=crt,*j=str;*j&&*i&&*i==*j;j++,i++);
            if (!*
    i){
                for(
    i=rep;*i;*trg++=*i++)str++;
                
    str=j;
            }else 
                *
    trg++=*str++;
        }
        *
    trg=0;
        return 
    temp;

    };

    void mainvoid 
    {
        
    char tmp[256] = "$user is cool\n";
        
    char username[80]="This code";
        
    replacestr(tmp"$user"usernametmp);
        
    printf(tmp);


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width