Created
February 1, 2014 17:17
-
-
Save bryanluby/8755416 to your computer and use it in GitHub Desktop.
NSString category for reversing a string.
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
// | |
// NSString+Reversed.h | |
// | |
// Created by Bryan Luby on 5/16/13. | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSString (Reversed) | |
// Reverses an NSString. Ex: "today" becomes "yadot" | |
+ (NSString *)bjl_reversedStringFromString:(NSString *)string; | |
@end |
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
// | |
// NSString+Reversed.m | |
// | |
// Created by Bryan Luby on 5/16/13. | |
// | |
#import "NSString+Reversed.h" | |
@implementation NSString (Reversed) | |
+ (NSString *)bjl_reversedStringFromString:(NSString *)string | |
{ | |
NSUInteger count = [string length]; | |
if (count <= 1) { // Base Case | |
return string; | |
} else { | |
NSString *lastLetter = [string substringWithRange:NSMakeRange(count - 1, 1)]; | |
NSString *butLastLetter = [string substringToIndex:count - 1]; | |
return [lastLetter stringByAppendingString:[self bjl_reversedStringFromString:butLastLetter]]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment