Challenge #2 - is it incorrect to use localizedCompare


#1

Hi,

I believe my solution is correct. However, I wasn’t sure if using localizedCompare was the right approach. Any feedback on this would be appreciated:

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        //Challenge is find common proper names that are also regular words
        // My thinking of algorithm:
        // - loop through all proper names
        // - take the proper name and convert it to lowercase (using NSstring to lowercaseString)
        //  - loop through all words dictionary
        //  - do a case SENSITIVE comparison to word...if it matches, then proper name has an equivalent regular word
        
        NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                                         encoding:NSUTF8StringEncoding
                                                            error:NULL];
        NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                         encoding:NSUTF8StringEncoding
                                                            error:NULL];
        
        // break it into an array of strings
        NSArray *names = [nameString componentsSeparatedByString:@"\n"];
        NSArray *words = [wordString componentsSeparatedByString:@"\n"];
        
        // Go through the array one string at a time
        // this is inefficient
        for (NSString *n in names) {
            
            NSString *lowerProperName = [n lowercaseString];
            
            for (NSString *w in words) {
                if ([w localizedCompare:lowerProperName] == NSOrderedSame) {
                    NSLog(@"Found that this propername - %@ matches the word %@", n, w);
                }
            }   
        }        
    }
    return 0;
}

Thank you for your help.