Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Created February 26, 2016 06:38
Show Gist options
  • Save jakehawken/482315e0a7d07fc15b1a to your computer and use it in GitHub Desktop.
Save jakehawken/482315e0a7d07fc15b1a to your computer and use it in GitHub Desktop.
Convert a string like "aabcccccbba" to one like "2a1b5c2b1a"
- (void)letterCounter:(NSString *)string {
NSMutableString *originalString = [NSMutableString stringWithString:string];
NSMutableString *outputString = [NSMutableString new];
while (originalString.length > 0)
{
unichar firstChar = [originalString characterAtIndex:0];
NSInteger characterCount = 1;
for (int i = 1; i < originalString.length; i++)
{
if ([originalString characterAtIndex:i] == firstChar)
{
characterCount++;
}
else
{
break;
}
}
[outputString appendString:[NSString stringWithFormat:@"%ld%c", characterCount, firstChar]];
NSRange range = NSMakeRange(0, characterCount);
[originalString deleteCharactersInRange:range];
}
printf("%s\n", [outputString UTF8String]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment