Hi there, I’ve only used one external file for this challenge — the one containing both proper names and regular words:
[code]//
// main.m
// InterestingNames
//
// Created by Antony Kwok on 23/10/2014.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Read a file as a huge string (ignoring the possibility for error)
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
// Break it into an array of strings
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
int index = 0; // (int)index is used as a progression tracker so that every (NSString *)n item only compares itself to posterior items in the array. Very important as it speeds up the running time considerably.
NSUInteger occurences = 0; // identical words counter
for (NSString *n in names) {
if ([n length] > 1) { // Excludes the letters of the alphabet
// Compare with items in the array following current (NSString *)n
for (int i = index + 1; i < index + 3 && i < [names count]; i++) {
if ([n caseInsensitiveCompare:names[i]] == NSOrderedSame) {
NSLog(@"%@ is the same as %@", n, names[i]);
occurences++;
NSLog(@"%lu", (unsigned long)occurences);
}
}
}
index++;
}
// How many identical occurences have been found?
NSLog(@"%lu identical occurences have been found.", (unsigned long)occurences);
}
return 0;
}
[/code]