Quick way to jumble the order of an NSString?

社会主义新天地 提交于 2019-12-07 14:04:29

问题


Does anyone know of an existing way to change the order of an existing NSString or NSMutableString's characters? I have a workaround in mind anyway but it would be great if there was an existing method for it.

For example, given the string @"HORSE", a method which would return @"ORSEH", @"SORHE", @"ROHES", etc?


回答1:


Consider this code:
.h File:

@interface NSString (Scrambling)

+ (NSString *)scrambleString:(NSString *)toScramble;

@end

.m File:

@implementation NSString (Scrambling)

+ (NSString *)scrambleString:(NSString *)toScramble {
   for (int i = 0; i < [toScramble length] * 15; i ++) {
      int pos = arc4random() % [toScramble length];
      int pos2 = arc4random() % ([toScramble length] - 1);
      char ch = [toScramble characterAtIndex:pos];
      NSString *before = [toScramble substringToIndex:pos];
      NSString *after = [toScramble substringFromIndex:pos + 1];
      NSString *temp = [before stringByAppendingString:after];
      before = [temp substringToIndex:pos2];
      after = [temp substringFromIndex:pos2];
      toScramble = [before stringByAppendingFormat:@"%c%@", ch, after];
   }
   return toScramble;
}

@end

Not the most beautiful code or execution, but gets the job done. There's probably a (const char *) way to do this, but this works fine for me. A quick test shows a 0.001021 second length for execution on my Mac.

Usage:

NSString *scrambled = [NSString scrambleString:otherString];

Code adapted from another language / pseudocode




回答2:


You can use Durstenfeld's variation of the Fisher-Yates Shuffle.

For a very long string, you could save a lot of CPU time and allocations by copying the unichars to a unichar buffer, then performing the transform using a c or c++ approach to swap characters. Note that the UTF8String is not the buffer you want to take, nor should you mutate it. Then create (or set) a new NSString from the shuffled buffer.

More info on the Fisher Yates algo and C and C++ implementations can be found here.



来源:https://stackoverflow.com/questions/9003182/quick-way-to-jumble-the-order-of-an-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!