Created
November 9, 2015 22:29
-
-
Save garmstro/3161fcc72e34d12ccfc2 to your computer and use it in GitHub Desktop.
NSString Extensions
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+StringExtensions.h | |
// StringFunctions | |
// | |
// Created by Geoff Armstrong on 10/29/15. | |
// Copyright © 2015 Geoff Armstrong. All rights reserved. | |
// | |
#ifndef NSString_StringExtensions_h | |
#define NSString_StringExtensions_h | |
@interface NSString (StringExtensions) | |
- (instancetype) reverseString; | |
- (instancetype) reverseParagraph; | |
/** | |
Function to convert string to an integer with checks for invalid characters and overflow | |
@return The integer, 0 if invalid | |
*/ | |
- (int) stringToInteger; | |
/** | |
Function to convert string to an array of all possible permutations of the characters in | |
the string. | |
@param theString The string to permute | |
@return Array containing all the string permutations | |
*/ | |
- (NSArray *) permutedStringArrayFromString: (NSString *) theString; | |
@end | |
#endif /* NSString_StringExtensions_h */ |
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+StringExtensions.m | |
// StringFunctions | |
// | |
// Created by Geoff Armstrong on 10/29/15. | |
// Copyright © 2015 Geoff Armstrong. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import "NSString+StringExtensions.h" | |
#define asciiShift 48 | |
@implementation NSString (StringExtensions) | |
- (instancetype) reverseString { | |
NSMutableString *revString = [[NSMutableString alloc] init]; | |
for (int i=0; i < self.length; i++){ | |
unichar character = [self characterAtIndex:(NSUInteger) i]; | |
NSString *charStr = [NSString stringWithCharacters:&character length:1]; | |
[revString insertString:charStr atIndex:0]; | |
} | |
return (NSString *)revString; | |
} | |
- (instancetype) reverseParagraph { | |
NSMutableString *revString = [[NSMutableString alloc] init]; | |
NSMutableArray *wordArray = [[NSMutableArray alloc] init]; | |
for (int i=0; i < self.length; i++) { | |
unichar character = [self characterAtIndex:i]; | |
if (character == ' ') { | |
//Add the word to the word array in reverse order | |
[wordArray insertObject:[NSString stringWithString:revString] atIndex:0]; | |
[revString setString:@""]; | |
} | |
else { | |
NSString *charStr = [NSString stringWithCharacters: &character length:1]; | |
[revString appendString: charStr]; | |
} | |
} | |
//At the end of the paragraph revString should already be set to the last word, just need to add a space | |
[revString appendString: @" "]; | |
for (int j=0; j < wordArray.count; j++) { | |
NSString *theWord = [wordArray objectAtIndex:j]; | |
[revString appendString: theWord]; | |
if (j < wordArray.count - 1) { | |
[revString appendString: @" "]; | |
} | |
} | |
return revString; | |
} | |
- (int) stringToInteger { | |
int newInt = 0; | |
BOOL isNegative = NO; | |
for (int i=0; i < [self length]; i++) { | |
unichar character = [self characterAtIndex:i]; | |
if (character == '-') { | |
isNegative = YES; | |
} | |
//Check to make sure it is a numeral | |
else if (character < 48 || character > 57) { | |
return 0; | |
} | |
else { | |
//Check for overflow | |
if (newInt > (INT_MAX - character + asciiShift) / 10) { | |
return 0; | |
} | |
//Multiply by 10 to shift digit left | |
newInt = newInt * 10; | |
//subtract 48 to convert ASCII character to the corresponding number | |
newInt += (character - asciiShift); | |
} | |
} | |
if (isNegative) { | |
newInt *= -1; | |
} | |
return newInt; | |
} | |
- (NSArray *) permutedStringArrayFromString: (NSString *) theString { | |
NSMutableArray *theArray = [[NSMutableArray alloc] init]; | |
//The length should never be 0 | |
if ([theString length] == 0) { | |
return theArray; | |
} | |
//The length will be 1 at the end of the string | |
if ([theString length] == 1) { | |
[theArray addObject:theString]; | |
return theArray; | |
} | |
NSString *remainingCharacters = [theString substringFromIndex:1]; | |
//Recursive call to this function will break each string down into smaller pieces until it reaches | |
//a single character | |
NSArray *remainingCharacterArray = [self permutedStringArrayFromString: remainingCharacters]; | |
//Get the first character of the string so that it can be placed at all spots within the array | |
unichar character = [theString characterAtIndex:0]; | |
NSString *firstCharacter = [NSString stringWithCharacters: &character length:1]; | |
for (int j=0; j < remainingCharacterArray.count; j++) { | |
for (int i=0; i <= remainingCharacters.length; i++) { | |
NSString *arrayString = [remainingCharacterArray objectAtIndex:j]; | |
NSMutableString *newString = [NSMutableString stringWithString:arrayString]; | |
[newString insertString:firstCharacter atIndex:i]; | |
[theArray addObject:newString]; | |
} | |
} | |
return theArray; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment