Challenge 2 Question


#1

Shouldn’t the results be the same regardless if I do

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

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

@autoreleasepool {
    
    // Read in file as a HUGE string
    NSString *nameString = [[NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                                     encoding: NSUTF8StringEncoding
                                                        error: NULL] lowercaseString];
    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"];
    
    NSLog(@"Number of Names: %lu\n", (unsigned long)[names count]);
    NSLog(@"Number of Words: %lu\n", (unsigned long)[words count]);
    
    NSInteger matches = 0;
    
    // Go through the array one string at a time
    for (NSString *n in names) {
        
        for (NSString *w in words) {
            
            // Was it found?
            if ([n isEqualToString: w]) {
                NSLog(@"%@", n);
                matches++;
            }
        }
        
    }
    
    NSLog(@"%ld", (long)matches);
    
}
return 0;

}
[/code] # of Matches: 294

OR this

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

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

@autoreleasepool {
    
    // Read in file as a HUGE string
    NSString *nameString = [[NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                                     encoding: NSUTF8StringEncoding
                                                        error: NULL] lowercaseString];
    NSString *wordString = [[NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                     encoding: NSUTF8StringEncoding
                                                        error: NULL] lowercaseString];
    
    // Break it into an array of strings
    NSArray *names = [nameString componentsSeparatedByString: @"\n"];
    NSArray *words = [wordString componentsSeparatedByString: @"\n"];
    
    NSLog(@"Number of Names: %lu\n", (unsigned long)[names count]);
    NSLog(@"Number of Words: %lu\n", (unsigned long)[words count]);
    
    NSInteger matches = 0;
    
    // Go through the array one string at a time
    for (NSString *n in names) {
        
        for (NSString *w in words) {
            
            // Was it found?
            if ([n isEqualToString: w]) {
                NSLog(@"%@", n);
                matches++;
            }
        }
        
    }
    
    NSLog(@"%ld", (long)matches);
    
}
return 0;

}
[/code]# of Matches: 1607

?

I don’t get why I would get different results for these.


#2

What is the difference between the two blocks of code? I can’t seem to find a difference.


#3

One is lowercaseStringing twice :slight_smile: