Replacing characters inside a string -
TwinkiDaBoss - 16.11.2016
Alright so I've stumbled upon a problem while creating this, I am simply trying to make something that will replace certain parts (random parts) of a string.
Example
Normal String:
Output should have 10 random characters:
Now currently my system is kinda glitchy.
1. It will replace few characters with exactly the same letter. For example it will replace 5 characters with a character S
2. Sometimes it will cut down the string. So for example you write "Hello world" and you will get the output "P"
pawn Код:
new LetterList[26][] =
{
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
};
stock strReplaceChar(string[128]) {
new text_lenght = strlen(string),
rr = random(text_lenght);
for(new i=0; i < text_lenght; i++) {
if(!strcmp(string, " ")) continue; //just to avoid cutting down the spaces
strreplace(string, string[rr], LetterList[random(sizeof(LetterList))], true, .maxlength = 128);
}
return string;
}
Re: Replacing characters inside a string -
SickAttack - 16.11.2016
PHP код:
new LetterList[] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
stock strReplaceChar(string[]) {
for(new i=0, j=strlen(string); i < j; i++) {
if(string[i] == ' ') continue; //just to avoid cutting down the spaces
string[i] = LetterList[random(sizeof(LetterList))];
}
return string;
}
Is that what you wanted?
Re: Replacing characters inside a string -
TwinkiDaBoss - 16.11.2016
Yep thats the one, thanks mate, I didnt even notice I was using strcmp also there, replaced that with strfind.
+Rep
Re: Replacing characters inside a string -
SickAttack - 16.11.2016
No need for strfind as you're comparing just one character on both ends, you can just compare both of the cells' numeric value.
You're welcome.
Re: Replacing characters inside a string -
TwinkiDaBoss - 16.11.2016
Yeah thats even better, thanks for the tip mate!
Re: Replacing characters inside a string -
SickAttack - 16.11.2016
Yeah, YW again!
Just another thing, warping a character in single quotes gives you its numeric value.
Re: Replacing characters inside a string -
TwinkiDaBoss - 16.11.2016
Damn bro I was going ham earlier wrapping everything in singlequotes, got too used to doing web development hahaha
Re: Replacing characters inside a string -
Vince - 16.11.2016
Quote:
Originally Posted by SickAttack
Yeah, YW again!
Just another thing, warping a character in single quotes gives you its numeric value.
|
If you want it even simpler, this works as well:
PHP код:
new letterList[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Or you can use minrandom/randomEx with 'A' through 'Z', cutting out the array entirely.