|
-
Oct 29th, 2001, 10:29 PM
#1
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++??
-
Oct 29th, 2001, 10:38 PM
#2
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 29th, 2001, 10:52 PM
#3
Well i guess i shouldn't be saying string but character array. It's not a string.
-
Oct 30th, 2001, 04:25 AM
#4
transcendental analytic
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.
-
Oct 30th, 2001, 07:39 PM
#5
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(playername, STRING(pEntity->v.netname));
replacestr(tmp, "$player", playername, tmp);
// Establish string and get the first token:
token = strtok( tmp, seps );
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?
-
Oct 30th, 2001, 08:03 PM
#6
Well here's some more examples:
This works:
PHP Code:
#include <string.h>
#include <stdio.h>
char* replacestr(const char* str,const char* crt,const char* rep,char*trg){
char* temp=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 main( void )
{
char tmp[256] = "$user is cool\n";
char username[80]="Me";
replacestr(tmp, "$user", username, tmp);
printf(tmp);
}
This doesn't
PHP Code:
#include <string.h>
#include <stdio.h>
char* replacestr(const char* str,const char* crt,const char* rep,char*trg){
char* temp=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 main( void )
{
char tmp[256] = "$user is cool\n";
char username[80]="This code";
replacestr(tmp, "$user", username, tmp);
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|