Raised This Month: $51 Target: $400
 12% 

Replace specific characters in a string?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Drak
Veteran Member
Join Date: Jul 2005
Old 12-05-2010 , 22:05   Replace specific characters in a string?
Reply With Quote #1

Example:
Code:
new MyString[] = "My very long sentence"
If i wanted was was above, to be randomized, and to look like this:
Code:
new MyString[] = "My very long sentence"
// do something

// my string would look like this
MyString[] = "Mv sery logn uentecne"
How would I go about doing that?
Basically just messing up the text abit. I tried usng "replace()" but that just started duplicating stuff.
__________________
Oh yeah
Drak is offline
Send a message via MSN to Drak
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-05-2010 , 22:13   Re: Replace specific characters in a string?
Reply With Quote #2

copy/move single characters from the source string to random positions in destination string without getting the same 'random' position twice.
__________________
fysiks is offline
Drak
Veteran Member
Join Date: Jul 2005
Old 12-05-2010 , 22:28   Re: Replace specific characters in a string?
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
copy/move single characters from the source string to random positions in destination string without getting the same 'random' position twice.
That's what I tried, I can't even get to the "copying" part.
Code:
	new Arg[64]
	read_args(Arg,63);
	
	server_print("Arg: %s",Arg);
	
	new NewArg[64]
	copy(Arg,63,Arg[6]);
	
	server_print("Arg: %s",NewArg);
I'm not sure how to 'select' a single char.
__________________
Oh yeah
Drak is offline
Send a message via MSN to Drak
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-05-2010 , 22:29   Re: Replace specific characters in a string?
Reply With Quote #4

Code:
	new NewArg[64]
	copy(Arg,63,Arg[6]);
NewArg is never used.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-05-2010 , 22:31   Re: Replace specific characters in a string?
Reply With Quote #5

Quote:
Originally Posted by Drak View Post
That's what I tried, I can't even get to the "copying" part.
I'm not sure how to 'select' a single char.
Just do direct character assignment (integer).

Code:
after[random_position_here] = before[i]
__________________
fysiks is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-05-2010 , 22:46   Re: Replace specific characters in a string?
Reply With Quote #6

Code:
stock RandomizeString(const string[], output[], const output_len) {     new string_len = strlen(string);         new Array:is_char = ArrayCreate(1);     new Array:chars = ArrayCreate(1);     new char_size;         for(new i = 0; i < string_len; i++)     {         if(isalpha(string[i] || isdigit(string[i]))         {             ArrayPushCell(chars, i);             ArrayPushCell(is_char, 1);             char_size++;         }         else         {             ArrayPushCell(is_char, 0);         }     }         new string_pos, output_pos, c, rand;     while(string_pos < string_len && output_pos < output_len)     {         if(ArrayGetCell(is_char, string_pos))         {             rand = random(char_size--);             c = ArrayGetCell(chars, rand);             ArrayDeleteItem(chars, rand);         }         else         {             c = string[string_pos];         }                 output[output_pos++] = c;         string_pos++;     }         ArrayDestroy(is_char);     ArrayDestroy(chars);         output[output_pos] = EOS;         return output_pos; }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-05-2010 , 22:58   Re: Replace specific characters in a string?
Reply With Quote #7

Wow, that looks complicated. I modified one of your non-repeating random functions:

PHP Code:
#include <amxmodx>

public plugin_init()
{
    
register_concmd("str""cmdScramble")
}

public 
cmdScramble()
{
    new 
szString[32]
    
read_argv(1szStringcharsmax(szString))
    
    new 
szOutput[32]
    
StringScramble(szStringszOutputcharsmax(szOutput))
    
    
server_print("Before: %s"szString)
    
server_print("After : %s"szOutput)
}

stock StringScrambleinput[], output[], output_len 

    new Array:
aNumbers ArrayCreate); 
    new 
input_len strlen(input); 
     
    for( new 
0input_leni++ ) 
    { 
       
ArrayPushCellaNumbers); 
    } 
     
    new 
iLastIndex mininput_lenoutput_len ); 
    new 
iRand
    for( new 
0iLastIndexi++ ) 
    { 
        
iRand randominput_len ); 
         
        
output[i] = input[ArrayGetCellaNumbersiRand )]; 
        
ArrayDeleteItemaNumbersiRand ); 
         
        
input_len--; 
    } 
     
    
ArrayDestroyaNumbers ); 
     
    return 
iLastIndex

__________________

Last edited by fysiks; 12-05-2010 at 23:09. Reason: Changed to test plugin (functional)
fysiks is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-05-2010 , 23:44   Re: Replace specific characters in a string?
Reply With Quote #8

Mine keeps the structure of the string by only replacing letters and numbers.
Yours will randomize all characters.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Drak
Veteran Member
Join Date: Jul 2005
Old 12-05-2010 , 23:45   Re: Replace specific characters in a string?
Reply With Quote #9

Awesome. Thanks guy's.
__________________
Oh yeah
Drak is offline
Send a message via MSN to Drak
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-05-2010 , 23:49   Re: Replace specific characters in a string?
Reply With Quote #10

Quote:
Originally Posted by Exolent[jNr] View Post
Mine keeps the structure of the string by only replacing letters and numbers.
Yours will randomize all characters.
Oh I see. I didn't think of that since he didn't explicitly say that he wanted that .
__________________
fysiks is offline
Reply



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 10:02.


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