When I read the description of of the words file, it says it contains regular words AND proper names. Based on the description in the book, I created this:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
int number=0;
// create a large string from the file
NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
// enter all the words into an array; words are separated by new line escape character
NSArray *words = [wordString componentsSeparatedByString:@"\n"];
// create a for loop using the number of elements in the array (count)
for (int i = 0; i < [words count]; i++){
// this if statement is to prevent any out of range exceptions
if (i > 0)
{
// this if statement converts the current element to lowercase then compares it to
// the word just before it (also converted to lowercase)
if ([[words[i] lowercaseString] isEqualToString:[words[i-1] lowercaseString]])
{
// prints the word and the proper name
NSLog(@"%@, %@", words[i], words[i-1]);
number++;
}
}
}
NSLog(@"Word count: %d", number);
}
return 0;
}
Am I reading the book wrong? I’m looking at everyone else’s solution and they are using both the propernames file and the words file but I don’t see the point in doing that if the words file contains them all anyway. Am I missing something in the way the book wants us to do this?
Here’s a sample of my solution:
2014-02-21 15:10:31.343 InterestingNames[11090:303] zeppelin, Zeppelin
2014-02-21 15:10:31.344 InterestingNames[11090:303] zeuglodon, Zeuglodon
2014-02-21 15:10:31.344 InterestingNames[11090:303] zipper, Zipper
2014-02-21 15:10:31.345 InterestingNames[11090:303] zoa, Zoa
2014-02-21 15:10:31.345 InterestingNames[11090:303] zwieback, Zwieback
2014-02-21 15:10:31.346 InterestingNames[11090:303] Word count: 1515