Raised This Month: $404 Target: $400
 101% 

Place a char before every char


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Papero
Senior Member
Join Date: Aug 2016
Location: Rimini, 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!
__________________

Last edited by Papero; 05-17-2018 at 13:09.
Papero is online now
Send a message via Skype™ to Papero
1337norway
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.
__________________
Taking Free/Paid Requests! Send a PM
--->Donate here if you like my work. <---
1337norway 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 13:50.


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