• 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: NSString, scramble
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString, scramble


  • Subject: Re: NSString, scramble
  • From: Michel Haver <email@hidden>
  • Date: Mon, 02 Jul 2001 23:09:21 +0200

On 7/1/01 15:26, "David Remahl" <email@hidden> wrote:

>> Hello,
>>
> Hi!
>
>> I try to get my feet wet by starting to port an Pascal application I
>> wrote
>> some years ago to cocoa. I have problems to find the correct statements
>> in
>> NSString, NSMutableString for something I can easily do in Pascal.
>>
>> Everything is simple if you know how to do it. Well although I loved the
>> cleanness of cocoa programming, translating my ideas in cocoa speak
>> isn't
>> natural to me at this moment in time. So I am asking for a little help
>> of
>> the experienced cocoa programmer.
>>
> Even though my knowledge in both Dutch and Pascal are fairly limited, I
> had a go...This is what I came up with. I wrote it using the cocoa
> classes, since you asked for it. However, pure C would also be a good
> option if speed is an option rather than clarity.
>
> NSMutableString* tempString = [NSMutableString
> stringWithString:@"heart"];
> NSMutableString* outString = [NSMutableString string];
> int randpos = 0;
> int currentLength;
> NSRange oneCharRange;
>
> srand(time(0));
>
> currentLength = [tempString length];
> while( currentLength )
> {
> oneCharRange = NSMakeRange(rand() % currentLength,1);
>
> [outString appendString:[tempString
> substringWithRange:oneCharRange]];
> [tempString deleteCharactersInRange:oneCharRange];
>
> currentLength--;
> }
>
> NSLog(@"In: %@ Out: %@",inString,outString);
>
> / Regards, David
David,

I am still bewildered by the quick answer on my question. I tried your
solution and it worked after I added
NSMutableString* inString = [NSMutableString
stringWithString:@"here is a string!"];
tempString = inString;

This is an elegant solution to my problem.

Learning by example code makes the steep learning more manageable, so is
there a depot with cocoa example code other than the ADC examples?

Cheers,

Michel



Adjusted source
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableString* inString = [NSMutableString
stringWithString:@"here is a string!"];
NSMutableString* tempString = [NSMutableString string];
NSMutableString* outString = [NSMutableString string];
//randpos isn't used
// int randpos = 0;
int currentLength;
NSRange oneCharRange;

srand(time(0));

NSLog(@"String in: %@.\n", inString);
//use instring as input for tempString
tempString = inString;

currentLength = [tempString length];
while( currentLength )
{
oneCharRange = NSMakeRange(rand() % currentLength,1);

[outString appendString:[tempString
substringWithRange:oneCharRange]];
[tempString deleteCharactersInRange:oneCharRange];
NSLog(@"String temp: %@.\n", tempString);
NSLog(@"String out: %@.\n", outString);
currentLength--;
}

NSLog(@"In: %@ Out: %@",inString,outString);
NSLog(@"Modified the strings...\n");
NSLog(@"String out: %@.\n", inString);
NSLog(@"String temp: %@.\n", tempString);
NSLog(@"String out: %@.\n", outString);


  • Prev by Date: Re: [HELP] How to change Desktop background pic in OSX from Cocoa-Java?
  • Next by Date: Re: NSString, scramble
  • Previous by thread: NSString, scramble
  • Next by thread: NSOpenPanel obsolete method?
  • Index(es):
    • Date
    • Thread