AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Place a char before every char (https://forums.alliedmods.net/showthread.php?t=307601)

Papero 05-17-2018 13:07

Place a char before every char
 
Hi,
I'm trying to do something like this:

abcde fgh
to
1a1b1c1d1e1 1f1h1g1h


Any ideas?

Maybe using regex or breaking the string?

Thanks!

backwards 05-17-2018 13:52

Re: Place a char before every char
 
I'm sure there's better ways than the following but with understanding the basic functions and logic you can become creative with many ways to a solution.

PHP Code:

stock PlaceCharInfrontOfAllCharsInString(String:input[512], char thechar)
{
    new 
maxlen = (strlen(input) * 2) + 1;
    if(
maxlen 512)
        return;
        
    
decl String:temp[512];
    
    for(
int i 0;i<strlen(input);i++)
        
Format(tempsizeof(temp), "%s%c%c"tempthecharinput[i]);
        
    
Format(input512"%s"temp);


PHP Code:

decl String:test[512] = "abcde fgh";
PlaceCharInfrontOfAllCharsInString(test'1');
PrintToServer(test); 

Note: I'm sure Format isn't meant to use a function argument variable to write to its own buffer variable and has undefined behavior and may be exploitable or cause memory corruption within the destination string.

Fyren 05-17-2018 15:02

Re: Place a char before every char
 
If you're going to loop over all the characters of the input, you don't want to Format every time. You only need to do two character assignments as you go along: out[i * 2] = separator and out[i * 2 + 1] = input[i].

Also, Format is able to use the same buffer for both input and output.


All times are GMT -4. The time now is 17:32.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.