Is there a way to sort an NSString?

Does anyone know if there is a way to sort NSString ASCII characters? Ideally, I need a method that checks if one line is a permutation of the other, so my idea is to sort both lines in a canonical way and then compare them. Any ideas would be highly appreciated. Thanks.

EDIT: That's what I'm more accurate. I need a method that takes two NSStrings as input and returns a BOOL :

 - (BOOL)isPermutation:(NSString *)string1 ofString:(NSString *)string2; 

The return value must be YES if one row can be rearranged to another row and NO otherwise.

NSStrings are arbitrary strings with ASCII characters , not sentences or numbers or words. Just arbitrary strings with ASCII characters.

+4
source share
3 answers

Do you really need to sort to verify this? Consider the algorithm.

  create 2 counter arrays, ac and bc, both of size 128
 initialize them with 0
 for each char c in string a make ac [c] ++
 for each char c in string b make bc [c] ++
 if all 128 counters in ac and bc are same, then they r permutation of one another

It may even work faster than sorting.

EDIT: This is a possible implementation. Since I did not compile the code, there may be small errors.

  - (BOOL) isPermutation: (NSString *) string1 ofString: (NSString *) string2 {
     if ([string1 length]! = [string2 length]) {
         return FALSE;    
     }

     NSInteger counter1 [128];
     NSInteger counter2 [128];
     NSInteger i;
     NSInteger len = [string1 length];

     for (i = 0; i <128; i ++) {
         counter1 [i] = counter2 [i] = 0;
     }

     for (i = 0; i <len; i ++) {
         unichar ch1 = [string1 characterAtIndex: i];
         unichar ch2 = [string2 characterAtIndex: i];
         counter1 [ch1] ++;
         counter2 [ch2] ++;
     }

     for (i = 0; i <128; i ++) {
         if (counter1 [i]! = counter2 [i]) {
             return FALSE;
         }
     }

     return TRUE;
 }
+7
source

If your characters are always ASCII, you can capture bytes, and then use one of the POSIX routines for them:

 char myCString[[myNSString length] + 1]; memcpy(myCString, [myNSString UTF8String], [myNSString length]); qsort(myCString, [myNSString length], 1, compareChars); 

Where compareChars() is the function you wrote to compare characters with a character - perhaps as simple as < in this case.

+2
source

Do you mean sorting letters inside a string? There is no method on NSString , but creating one would be pretty easy. Here's an example of a quick and dirty one (you may need to adapt it to your goals):

 #import <Foundation/Foundation.h> #include <stdlib.h> #include <stdio.h> #include <string.h> static int compare_char(const char *a, const char *b) { if (*a > *b) { return 1; } else if (*a < *b) { return -1; } else { return 0; } } @interface NSString (Sorting) - (NSString *)stringBySortingCharacters; @end @implementation NSString (Sorting) - (NSString *)stringBySortingCharacters { const char *s = [self UTF8String]; char *s2 = (char *) calloc([self length]+1, 1); if (!s2) return nil; strncpy(s2, s, [self length]); qsort(s2, [self length], 1, compare_char); NSString *ret = [NSString stringWithUTF8String:s2]; free(s2); return ret; } @end int main(int argc, char **argv) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *s1 = @"string"; NSString *s2 = @"the quick brown fox jumps over the lazy dog"; printf("Sorted: %s\n", [[s1 stringBySortingCharacters] UTF8String]); printf("Sorted: %s\n", [[s2 stringBySortingCharacters] UTF8String]); [pool release]; return 0; } 
+2
source

Source: https://habr.com/ru/post/1346097/


All Articles