dimanche 8 février 2015

Randomly selecting specific subsequence from string

Given a string containing a number of characters interspersed with dashes, for example string s = "A--TG-DF----GR--";, I wish to randomly select a block of dashes (could be of size 1, 2, ..., max number of consecutive dashes in string), and copy them over to another part of the string chosen at random.


For example, A--TG-DF(---)-GR-- gets moved to A--T(---)G-DF-GR--


Another iteration may give A--TG-DF----GR(--) gets moved to A--TG-(--)DF----GR


I'm generating random indices of the string through int i = rand()%(int)s.length(); Insertion happens through s.insert(rand()%(int)s.length(), substr);, where substr is the block of dashes.


My main problem lies in finding randomly a continuous group of dashes. I thought of using s.find("-"), but that'd only return the first instance of a single dash, and not a random position of a collection of dashes.


Thanks a lot.





Aucun commentaire:

Enregistrer un commentaire