• 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: Robert Marini <email@hidden>
  • Date: Wed, 31 Dec 2008 01:22:42 -0500

In general, it would perhaps be best to create a c string from the NSString using the proper text encoding and then iterating through the C string in reverse, appending each character to an NSMutableString. Iterating through each character of an NSString is likely to be slow and the usage of characterAtIndex is certainly more than pointer arithmetic.

-rob.

On Dec 31, 2008, at 12:59 AM, Adam R. Maxwell wrote:


On Dec 31, 2008, at 4:23 PM, Ron Fleckner wrote:
	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;
	}

Unfortunately, this is not correct; -[NSString characterAtIndex:] returns a unichar, which is not a char. In addition, it will give odd results for composed characters. Depending on what you want, you might be able to use rangeOfComposedCharacterAtIndex:.


I'd also use NSMutableString instead of a stack buffer of chars, since this looks like a buffer overflow (unless I'm missing something):

reverseChars[j] = '\0';// create a C (UTF8) string by adding a null char


--
Adam

_______________________________________________

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

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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: "Adam R. Maxwell" <email@hidden>)

  • Prev by Date: Re: Reversing a String
  • Next by Date: Re: Reversing a String
  • Previous by thread: Re: Reversing a String
  • Next by thread: Re: Reversing a String
  • Index(es):
    • Date
    • Thread