Seems like it should work - Challenge Part 2


#1

Here’s the code I have for challenge part 2.

[code]#import <Foundation/Foundation.h>

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

@autoreleasepool {
    
    // Read in the proper names file
    NSString *properNamesFile = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                                          encoding:NSUTF8StringEncoding
                                                             error:NULL];

    // Read in the words file
    NSString *wordsFile = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                    encoding:NSUTF8StringEncoding
                                                       error:NULL];
    
    // Break the files into two arrays of strings
    NSArray *properNames = [properNamesFile componentsSeparatedByString:@"\n"];
    NSArray *words = [wordsFile componentsSeparatedByString:@"\n"];
    
    for (NSString *name in properNames) {
        for (NSString *word in words) {
            if ([name caseInsensitiveCompare:word] == NSOrderedSame) {
                NSLog(@"Word %@ and Name %@ are equal", word, name);
            }
        }
    }
    
}
return 0;

}[/code]

Seems like this should work fine, but it doesn’t. Any suggestions?

Thank you


#2

I have it figured out that all the names appear in words list which is why they all match between the two files.

Is the challenge really just to take the words file and compare one word to the next until it finds two words the same? That was my original plan but so many answers on here were loading in both files.