• 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 16:23:34 +1100


On 31/12/2008, at 4:08 PM, Gabe Shahbazian wrote:

What is the best way to take an NSString and reverse the characters so that
"hello" becomes "olleh"?
Thanks for any and all help,
Gabe S

Hi Gabe.

I don't think there's anything in Cocoa that does this for you. I needed to reverse strings and came up with this:

- (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;
}

HTH,
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


  • Follow-Ups:
    • Re: Reversing a String
      • From: Nick Zitzmann <email@hidden>
    • Re: Reversing a String
      • From: "Adam R. Maxwell" <email@hidden>
    • Re: Reversing a String
      • From: Graham Cox <email@hidden>
References: 
 >Reversing a String (From: "Gabe Shahbazian" <email@hidden>)

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