Challenge #2

To solve this challenge, I decided to convert both the proper names and the common words to lowercase using the lowercaseString message. I know that the common words are already lowercase, but when I convert both lists to lowercase, my program does not work. However, if I only convert the proper names to lowercase and leave the common words alone, the program works and finds 293 matches. Does anyone know the reason why sending the lowercaseString message to the common words does not work (the way I think it should)?

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

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

@autoreleasepool {
    
    // Read in the proper names file as a huge string (ignoring the possibility of an error)
    NSString *properNamesString =
    [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                              encoding:NSUTF8StringEncoding
                                 error:NULL];
    
    // Convert the proper names to lower case and then break it up into an array
    properNamesString = [properNamesString lowercaseString];
    NSArray *properNames = [properNamesString componentsSeparatedByString:@"\n"];
    
    
    // Read in the common words file as a huge string (ignoring the possibility of an error)
    NSString *commonWordsString =
    [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                              encoding:NSUTF8StringEncoding
                                 error:NULL];
    
    // Convert the common words to lower case and then break it up into an array
    commonWordsString = [commonWordsString lowercaseString];
    NSArray *commonWords = [commonWordsString componentsSeparatedByString:@"\n"];
    
    // Create an array to store the words that are in both the proper names and common words lists
    NSMutableArray *matches = [[NSMutableArray alloc] init];
    
    // Go Loop through both word lists looking for matches
    for (NSString *name in properNames) {
                
        for (NSString *words in commonWords) {

            if ([name isEqualToString: words]) {
                NSLog(@"%@ was added to matchesArray", name);
                [matches addObject: words];
            }
        }
    }
    
    NSLog(@"%@", matches);
    NSLog(@"There are %lu matches", (unsigned long)[matches count]);
}
return 0;

}[/code]

You mean you are finding more matches? By lowercasing the common words, you are probably creating duplicates.

No, I’m asking why is it adding every single word to the matches array?