In Objective C this can be done as follows:
-(NSString *)decode:(NSString *)input {
NSArray<NSString *> *components = [input componentsSeparatedByString:@"-"];
NSMutableString *output = [[NSMutableString alloc] init];
for (NSString *component in components) {
if (component.length == 0) {
continue;
}
char charValue = (char) [component intValue];
[output appendFormat:@"%c", charValue + ('a' - 1)];
}
return [NSString stringWithString:output];
}
Note that this assumes the input already has the correct format. It will accept strings of the form "--42-df-w---wf"
happily.
EDIT: If there are multiple possible separators (e.g. hyphen and space), you can use NSCharacterSet
:
-(NSString *)decode:(NSString *)input {
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString: @"- "];
NSArray<NSString *> *components = [input componentsSeparatedByCharactersInSet:separatorSet];
...
}
8
solved How can I decode an NSString using the A1Z26 cipher? [closed]