Re: Reversing a String
Re: Reversing a String
- Subject: Re: Reversing a String
- From: Nick Zitzmann <email@hidden>
- Date: Tue, 30 Dec 2008 23:26:43 -0700
On Dec 30, 2008, at 10:23 PM, Ron Fleckner wrote:
- (NSString *)reverseString:(NSString *)aString
{
// Do the reversing with the help of an array of chars
int i, j;
const int stringLength = [aString length];
char reverseChars[stringLength];
for (i = stringLength - 1, j = 0; i >= 0; i--, j++)
{
char c = [aString characterAtIndex:i];
reverseChars[j] = c;
}
reverseChars[j] = '\0';// create a C (UTF8) string by adding a null
char
NSString *backwardsString = [NSString
stringWithUTF8String:reverseChars];
return backwardsString;
}
There are a few problems with this method, e.g. it won't work with non-
ASCII characters, it'll probably blow up if the string is ~10,000
characters or longer, the ints need to be NSUIntegers, and this ought
to be a category method that works on the receiver. I'd use unichar,
NSUInteger, malloc(), and -
initWithCharactersNoCopy:length:freeWhenDone: at the least...
Nick Zitzmann
<http://www.chronosnet.com/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden