Please check the logic: getting 294 as answer


#1

Does this code look ok?
I’m getting 294 as the answer:

[code]//
// main.m
// Challenge 2861
//
// Created by Pareto on 10/6/14.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    // Read in propernames file as a huge string (ignoring the possibility of an error)
    NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                                     encoding:NSUTF8StringEncoding
                                                               error:NULL];
    
    // Make it all lower case, since we will be looking for exact matches later
    NSString *nameStringAllLower = [nameString lowercaseString];
    
    // Break it into an array of strings
    NSArray *names = [nameStringAllLower componentsSeparatedByString:@"\n"];
    
    // Read in words file as a huge string (ignoring the possibility of an error)
    NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                     encoding:NSUTF8StringEncoding
                                                        error:NULL];
    // Break it into an array of strings
    NSArray *words = [wordString componentsSeparatedByString:@"\n"];
   
    int numNames = 0;
    
    for (NSString *n in names) {
        for (NSString *w in words) {
            // does the name match the word? Remember, name has been made lowercase
            BOOL same = [n isEqualToString:w];
        
            // Was it found?
            // if so, there's a word matching the name which was converted to lowercase
            if (same) {
                numNames++;
                NSLog(@"%@", n );
                break;
            }
        }
    }

    NSLog(@"Number of proper names that are also regular words = %d", numNames);
}
return 0;

}
[/code]


#2

That should be 293, not 294.

The reason? Both arrays contain a null word each, and you are counting them in.

[Become a competent programmer faster than you can imagine: pretty-function.org]