• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Reversing a String
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reversing a String


  • Subject: Re: Reversing a String
  • From: Ron Fleckner <email@hidden>
  • Date: Wed, 31 Dec 2008 18:41:05 +1100


On 31/12/2008, at 5:26 PM, Nick Zitzmann wrote:


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/>

Yes of course, you're right. Perhaps I should have pointed that out to the OP but didn't think of it because I use it in a app which is a pale clone of Accessorizer, that is, the string it's reversing is a type and variable declaration. Reading it backwards gives me the variable name before any asterisks or types, so I can work out (in the code) quickly whether the variable is a pointer or a primitive type.


Ron
_______________________________________________

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


References: 
 >Reversing a String (From: "Gabe Shahbazian" <email@hidden>)
 >Re: Reversing a String (From: Ron Fleckner <email@hidden>)
 >Re: Reversing a String (From: Nick Zitzmann <email@hidden>)

  • Prev by Date: Re: UISlider changes between 2.1 and 2.2, rating widget?
  • Next by Date: Storing NSDocument data in packages/bundles
  • Previous by thread: Re: Reversing a String
  • Next by thread: Re: Reversing a String
  • Index(es):
    • Date
    • Thread