Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Alkerak

macrumors newbie
Original poster
Jul 20, 2011
13
0
Is there a way to convert an NSString to an Array that holds all individual letters of the string? I am planning for example to scramble the letters.

So I want to input a string, break it down to individual letters and scramble the letters.
Any ideas?
 
Break the problem down into parts. Solve each part. Combine.

Part 1.
Break an NSString into parts (substrings). Each substring should consist of one character. Refer to the NSString class reference doc and look for methods that produce substrings. Examples:
substringFromIndex:
substringToIndex:
substringWithRange:
Part 2.
Put the string parts (substrings) into an array. Refer to the NSMutableArray class reference doc and look for methods that add objects to the array.

Part 3.
Scramble the array. This is left as an exercise for the reader.
Possible search terms related to scramble: shuffle.

Part 4.
Combine the scrambled array parts into an NSString. This is also left as an exercise.
You could start by looking for NSString and NSArray methods that combine parts together.
 
Before chown33

I like chown33's approach, but would add one step before:

Part 0.
Check the Cocoa documentation (probably NSString in this case) to see if the hard working, dedicated software engineers at Apple have already solved your problem. Look at the methods in NSString or NSArray or NSMutuableArray to see if there's a arrayFromString or similarly named method.

In this case I don't think that method exists, but when it does, yea!
 
Thank you for the advice.
I think I am getting the hang of it. It seems to my solution is almost complete.
Here is a preliminary "review" of the situation which works perfectly.
I just need to store the strings into an Array and thats it.
My main problem is, coming from a C background, I cannot envision this program working OOP way. I still think of it without OOP.

Code:
#import <Foundation/Foundation.h>
#import "IOUtility.h"

int main (int argc, const char * argv[])
{

   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   NSString *string = ReadFromStandardInputWithNewLine(NO);
   
   NSUInteger intLength = [string length];
   NSInteger i = 0;
   NSRange range;
   range.length = 1;

   while (i <= (intLength - 1))
   {
      range.location = i;
      NSString *tmp = [string substringWithRange:range];
      NSString *singleCharString = [tmp stringByAppendingFormat:@"\n"];
      WriteToStandardOutput(singleCharString);
      i++;
   }

   [pool drain];
   return 0;
}
 
It's no problem in a tiny bit of code like this, but:

For example, the range that you are extracting is defined in one line, then the length is defined somewhere else, then the start is defined in a third place... To find out what the substring method actually does, I need to check in three places. Can you create a single line that declares and sets "range", just before it is used?

The variable "i" is worse. There is a dozen lines between initialization and increment. You could just write

for (NSInteger i = 0; i < intLength; ++i) { ... }

or

for (NSInteger i = 0; i < [string length]; ++i) { ... }

That way the reader can see immediately what the range of the loop is, by checking a single statement, and not three statements that are far apart.
 
How would I create a single line that declares and sets the range?
It is a structure ...
 
Did it.

Now I will concentrate on adding space detection so it will delimit scrambling on a word basis and then some user interface (text only) then Cocoa interface.

Thanks for the help! :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.