Challenge 2 - Need comments


#1

Guys,

This is my solution. Quite easy to understand and hopefully correct. I really appreciate your constructing comments. Thanks.

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        // Read in a file as a huge string
        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 separate array of strings
        NSArray *names = [nameString componentsSeparatedByString:@"\n"];
        NSArray *words = [wordString componentsSeparatedByString:@"\n"];
        
        // Go through the names array one string at a time
        for (NSString *n in names) {
            // Go through the words array one string at a time
            for (NSString *w in words) {
                // Check if the string in the names is the same with the string in the words
                if ([n caseInsensitiveCompare:w] == NSOrderedSame) {
                    // Check if the string in the names is present earlier than in the words, if earlier, it is a name
                    if ([n compare:w] == NSOrderedAscending) {
                        NSLog(@"%@ is a name, and %@ is not",n,w);
                    }
                }
            }

        }

    }
    return 0;
}