Raised This Month: $90 Target: $400

Place a char before every char

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

I'm trying to do something like this:

abcde fgh
1a1b1c1d1e1 1f1h1g1h

Any ideas?

Maybe using regex or breaking the string?


Last edited by Papero; 05-17-2018 at 14:09.
Papero is offline
Send a message via Skype™ to Papero
AlliedModders Donor
Join Date: Feb 2014
Location: USA
Old 05-17-2018 , 14: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)
maxlen = (strlen(input) * 2) + 1;
maxlen 512)
decl String:temp[512];
int i 0;i<strlen(input);i++)
Format(tempsizeof(temp), "%s%c%c"tempthecharinput[i]);

PHP Code:
decl String:test[512] = "abcde fgh";
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
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 05-17-2018 , 16: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 16:03.
Fyren is offline

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 03:58.

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