Replacing characters inside a string
#1

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:
Quote:

Hello World

Output should have 10 random characters:
Quote:

Aqer Zxcas

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;
}
Reply
#2

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=0j=strlen(string); ji++) {
        if(
string[i] == ' ') continue; //just to avoid cutting down the spaces
        
string[i] = LetterList[random(sizeof(LetterList))];
    }
    return 
string;

Is that what you wanted?
Reply
#3

Yep thats the one, thanks mate, I didnt even notice I was using strcmp also there, replaced that with strfind.

+Rep
Reply
#4

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.
Reply
#5

Yeah thats even better, thanks for the tip mate!
Reply
#6

Yeah, YW again!

Just another thing, warping a character in single quotes gives you its numeric value.
Reply
#7

Damn bro I was going ham earlier wrapping everything in singlequotes, got too used to doing web development hahaha
Reply
#8

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)