1

hi i want to display string combination on nslog like this format

abc
bca
acb
and so on 

but my program show me it like this format permutations =

 (
        (
        c,
        b,
        a
    ),
        (
        b,
        c,
        a
    ),
        (
        c,
        a,
        b
    ),
        (
        a,
        c,
        b
    ),
        (
        b,
        a,
        c
    ),
        (
        a,
        b,
        c
    )
)

this is the code i am using

NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];

NSMutableArray *permutations = nil;

int i = 0;
for (i = 0; i < array.count ; i++){

    if (!permutations){
        permutations = [NSMutableArray array];
        for (NSString *character in array){
            [permutations addObject:[NSArray arrayWithObject:character]];


        }

    } else {

        //make copy of permutations array and clean og array
        NSMutableArray *aCopy = [permutations copy] ;
        [permutations removeAllObjects];

        for (NSString *character in array){

            //loop through the copy
            for (NSArray *oldArray in aCopy){

                //check if old string contains looping char..
                if ([oldArray containsObject:character] == NO){

                    //update array
                    NSMutableArray *newArray = [NSMutableArray arrayWithArray:oldArray];
                    [newArray addObject:character];

                    //add to permutations
                    [permutations addObject:newArray];

                }

            }
        }            
    }



}

NSLog(@"permutations = \n %@",permutations);
}

kindly tell me how i can display it in my require format

Azy Qadir
  • 155
  • 1
  • 3
  • 12

4 Answers4

2

Try this way :

NSString *stringPermuted=[NSString new];
for (NSArray *array in permutations) {
    for (NSString *string in array) {
        stringPermuted=[stringPermuted stringByAppendingFormat:@"%@",string];
    }
    stringPermuted=[stringPermuted stringByAppendingFormat:@"\n"];
}

NSLog(@"permutations=\n%@",stringPermuted);

Another Way :

NSString *stringPermuted=[NSString new];
for (NSArray *array in permutations) {
    stringPermuted=[stringPermuted stringByAppendingFormat:@"%@\n",[array componentsJoinedByString:@""]];
}
NSLog(@"permutations=\n%@",stringPermuted);
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
1

Create a simple method for displaying

- (NSString *)formatPermutations:(NSArray *)permutations {
    NSString * formattedPermutations = @"";
    for (NSArray * permutation in permutations) {
        formattedPermutations = [formattedPermutations stringByAppendingFormat:@"%@\n", [self formatPermutation:permutation]];
    }
    return formattedPermutation;
}

- (NSString *)formatPermutation:(NSArray *)permutation {
    NSString * formattedPermutation = @"";
    for (NSString * letter in permutation) {
        formattedPermutation = [formattedPermutation stringByAppendingString:letter];
    }
    return formattedPermutation;
}

and use it

NSLog(@"permutations = \n %@",[self formatPermutations:permutations]);

Another option (and maybe preferable option) would be to create your own classes and PermutationArray and Permutation and override their description method.

Such method is the equivalent of a Java toString and it gets called whenever NSLog needs to get a NSString representation out of an object.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
0

With your last line NSLog(@"permutations = \n %@",permutations); you are logging the entire NSMutableArray in the console. Xcode is formatting it like that to make it more readable.

Try logging your results this way:

Updated version:

for (NSArray *permutation in permutations){
    NSMutableString *tempString = [[NSMutableString alloc] initWithString:@""];
    for(NSString *character in permutation){
        [tempString appendString:character]
    }
    NSLog(@"%@\n",tempString);
}
Nikola Kirev
  • 3,152
  • 3
  • 22
  • 30
0

Your permutations object is an NSArray of NSArrays. When using NSLog to log it, NSLog will invoke the description method of NSArray and this uses linebreaks to split up eacho f the objects in the array.

If you want to change the way it is printed you can either use your own NSArray subclass and overwrite the description method or just write a slightly more complicated log statement like so:

Quick and Dirty

for (NSArray *arrayOfStrings in permutations) {
    for (NSString *oneCharacterString in arrayOfStrings) {
        printf("%s", [oneCharacterString UTF8String]);
    }
    printf(" ");
}

Little bit less quick a lot less dirty

// with linebreaks after each permutation
// If you don't want those, move the NSLog out of the for loop construct one big string to log in the format you like
for (NSArray *arrayOfStrings in permutations) {
    NSMutableString *permutationString = [NSMutableString string];
    for (NSString *oneCharacterString in arrayOfStrings) {
        [permutationString appendString:oneCharacterString]
    }
    NSLog(permutationString);
}

Other comments: If you want to save only onecharacter anyway you could also use an NSNumber object (create it using [NSNumber numberWithChar:]). Or you could use NSString or NSMutableString instead of the inner arrays.

Joachim Kurz
  • 2,875
  • 6
  • 23
  • 43
  • `printf` doesn't apply here. I mean, it's correct, but you shouldn't use it for logging. Return a `NSString` instead, then log it using `NSLog`. – Gabriele Petronella Jan 10 '13 at 14:22
  • @GarielePetronella why not? Just tested it, seems to work? I used printf to avoid NSLogs linebreaks. – Joachim Kurz Jan 10 '13 at 14:26
  • `NSLog` is meant for logging and has many useful features supporting this. `printf` is a general C function writing to `stdout`. More info here: http://stackoverflow.com/questions/9006801/difference-between-nslog-and-printf-statement-for-objectivec – Gabriele Petronella Jan 10 '13 at 14:27
  • Okay, you could argue, that you shouldn't use printf in Objective-C code to keep it clean. Depends on whether you are just quickly testing something or using it in a "more professional" way. Creating a string object as in the example with `formatPermutation:` is probably nicer. – Joachim Kurz Jan 10 '13 at 14:28
  • "Quick" doesn't necessarily mean "dirty". `NSLog` use case nicely apply here so I would suggest to use it. Also it avoids the conversion to `char *` you had to perform using `UTF8String`. – Gabriele Petronella Jan 10 '13 at 14:31
  • oh thanks very much but you show it horizontally in one one but i wanted to show it verticaly – Azy Qadir Jan 10 '13 at 14:37
  • @AzyQadir Yes, I read the first draft of your question and assumed you wanted them all on one line (that's also why I used printf). I added another one with NSLog and linebreaks between the permutations. – Joachim Kurz Jan 10 '13 at 14:44
  • 1
    @GabrielePetronella ok, I added a similar one with NSLog, you convinced me ;-) – Joachim Kurz Jan 10 '13 at 14:45