Created
February 26, 2016 06:38
-
-
Save jakehawken/482315e0a7d07fc15b1a to your computer and use it in GitHub Desktop.
Convert a string like "aabcccccbba" to one like "2a1b5c2b1a"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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