Here’s my solution for challenge 2.
My view on this was that all the names are capitalised, so convert the big long name string to all lower case, then perform a case sensitive (which I think is the default?) search.
Anyway, heres my code:
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Read in lines as a huge string (ignoring the possibility of an error
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding
error:NULL];
NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
// Convert to lower case string
NSString *nameStringLC = [nameString lowercaseString];
// Break it into an array of strings
NSArray *lowercaseNames = [nameStringLC componentsSeparatedByString:@"\n"];
// Go through the array one string at a time
NSUInteger wordCount = 0;
for (NSString *i in lowercaseNames) {
NSRange r = [wordString rangeOfString:i];
// Was it found?
if (r.location != NSNotFound) {
// NSLog(@"%@", i);
wordCount++;
}
}
// Print the value of wordCount which should be the number of names listed in lowercase in the Words file
NSLog(@"%lu", (long)wordCount);
}
return 0;
}
Total ended up being 692… Does this match anyone elses result?