Use an NSScanner to move through the string and substitute each character as it is found. This way, all substitutions are done in one pass and you're never looking at a position twice.
NSMutableString * fixedUpString = [NSMutableString string];
NSScanner * scanner = [NSScanner scannerWithString:origString];
NSCharacterSet * subCharacters = [NSCharacterSet characterSetWithCharactersInString:@"*/"];
while( ![scanner isAtEnd] ){
// Pick up other characters.
NSString * collector;
if( [scanner scanUpToCharactersInSet:subCharacters intoString:&collector] ){
[fixedUpString appendString:collector];
}
// This can easily be generalized with a loop over a mapping from
// found characters to substitutions
// Check which one we found
if( [scanner scanString:@"*" intoString:nil] ){
// Append the appropriate substitution.
[fixedUpString appendString:@"/"];
}
else /* if( [scanner scanString:@"/" intoString:nil] ) */ {
[fixedUpString appendString:@"*"];
}
}
fixedUpString now contains the substituted content.
As I noted in the comment, this can be generalized very easily to any number of substitutions:
NSDictionary * substitutions = @{ @"a" : @"z", @"b" : @"y", ... };
NSCharacterSet * keyChars = [NSCharacterSet characterSetWithCharactersInString:[[substitutions allKeys] componentsJoinedByString:@""]];
...
// Check which one we found
for( NSString * keyChar in [substitutions allKeys] ){
if( [scanner scanString:keyChar intoString:nil ){
[fixedUpString appendString:substitutions[keyChar]];
break;
}
}