Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Created February 26, 2016 17:29
Show Gist options
  • Save jakehawken/759d0fd7523f12b6a329 to your computer and use it in GitHub Desktop.
Save jakehawken/759d0fd7523f12b6a329 to your computer and use it in GitHub Desktop.
Convert a string like "aabcccccbba" to one like "2a1b5c2b1a" more efficiently than this one: https://gist.github.com/jakehawken/482315e0a7d07fc15b1a
- (void)letterCounter:(NSString *)originalString
{
NSMutableString *outputString = [NSMutableString new];
NSInteger stringIndex = 0;
while (stringIndex < originalString.length)
{
unichar currentChar = [originalString characterAtIndex:stringIndex];
NSInteger characterCount = 1;
for (NSInteger i = stringIndex + 1; i < originalString.length; i++)
{
if ([originalString characterAtIndex:i] == currentChar)
{
characterCount++;
}
else
{
break;
}
}
[outputString appendString:[NSString stringWithFormat:@"%ld%c", characterCount, currentChar]];
stringIndex += characterCount;
}
printf("%s\n", [outputString UTF8String]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment