vendredi 6 octobre 2017

MATLAB Loop to Randomly Modify String Characters

I’ve started programming in MATLAB recently and tried implementing a 1337 speaker-like generator/string manipulator just for fun. As a challenge, I’m trying to randomly change the occurrence of each character so that not all “a” are changed to “@“.

It appears that my attempt works to some extend since it's very arbitrary (yet sometimes ineffective), but I believe there’s a better way of accomplishing this. Perhaps to add more alternatives for each of the 26 characters and randomly select from them, respectively?

function O = obfuscate(s)

str = 'hello world';

for i=1:length(str)
    randomNum = randi(26,1);

    switch randomNum
        case 1            
            str = regexprep(str, 'a', '@', 'once');
        case 2
            str = regexprep(str, 'b', 'l3', 'once');
        case 3
            str = regexprep(str, 'c', '<', 'once');
        case 4
            str = regexprep(str, 'd', '|]', 'once');
        case 5
            str = regexprep(str, 'e', '3', 'once');
        case 6
            str = regexprep(str, 'f', '|#', 'once');
        case 7
            str = regexprep(str, 'g', '6', 'once');
        case 8
            str = regexprep(str, 'h', '|-|', 'once');
        case 9
            str = regexprep(str, 'i', '!', 'once');
        case 10
            str = regexprep(str, 'j', '_/', 'once');
        case 11
            str = regexprep(str, 'k', '|{', 'once');
        case 12
            str = regexprep(str, 'l', '1', 'once');
        case 13
            str = regexprep(str, 'm', '|\/|', 'once');
        case 14
            str = regexprep(str, 'n', '/\/', 'once');
        case 15
            str = regexprep(str, 'o', '[]', 'once');
        case 16
            str = regexprep(str, 'p', '|*', 'once');
        case 17
            str = regexprep(str, 'q', '9', 'once');
        case 18
            str = regexprep(str, 'r', '|2', 'once');
        case 19
            str = regexprep(str, 's', '$', 'once');
        case 20
            str = regexprep(str, 't', '+', 'once');
        case 21
            str = regexprep(str, 'u', '|_|', 'once');
        case 22 
            str = regexprep(str, 'v', '\/', 'once');
        case 23
            str = regexprep(str, 'w', '\X/', 'once');
        case 24
            str = regexprep(str, 'x', '%', 'once');
        case 25
            str = regexprep(str, 'y', '¥', 'once');
        case 26
            str = regexprep(str, 'z', '2', 'once');
    end

   O = str;

    %fprintf('%s(%i)', str(i), randomNum);  
end

Any suggestions?




Aucun commentaire:

Enregistrer un commentaire