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