Raised This Month: $32 Target: $400
 8% 

Place a char before every char


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Papero
Veteran Member
Join Date: Aug 2016
Location: Italy
Old 05-17-2018 , 13:07   Place a char before every char
Reply With Quote #1

Hi,
I'm trying to do something like this:

abcde fgh
to
1a1b1c1d1e1 1f1h1g1h


Any ideas?

Maybe using regex or breaking the string?

Thanks!
__________________
My Plugins
SPCode


Steam: hexer504
Telegram: Hexah
Discord: Hexah#6903

If you like my work you can donate here!

Last edited by Papero; 05-17-2018 at 13:09.
Papero is offline
backwards
AlliedModders Donor
Join Date: Feb 2014
Location: USA
Old 05-17-2018 , 13:52   Re: Place a char before every char
Reply With Quote #2

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.
__________________
I highly recommend joining the SourceMod Discord Server for real time support.
backwards is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 05-17-2018 , 15:02   Re: Place a char before every char
Reply With Quote #3

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.

Last edited by Fyren; 05-17-2018 at 15:03.
Fyren is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:37.


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