Skip to content

Instantly share code, notes, and snippets.

@PaulSolt
Created May 6, 2013 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulSolt/5523000 to your computer and use it in GitHub Desktop.
Save PaulSolt/5523000 to your computer and use it in GitHub Desktop.
Chapter 15 Solution: Objective-C Programming Guide Ed. 1. The book says that words doesn't contain proper names, but it does. We can check for non-proper names that match by testing isEqualToString: and having it fail. -Paul Solt
//
// main.m
// FindWords
//
// Created by Paul Solt on 5/5/13.
// Copyright (c) 2013 Paul Solt. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
for(NSString *name in names) {
NSRange r = [name rangeOfString:@"AA" options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound) {
NSLog(@"Double AA: %@", name);
}
}
// Proper names are in the word file
NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
NSArray *words = [wordString componentsSeparatedByString:@"\n"];
// Search for a word that's in both, using case insensitive search
NSLog(@"Word count: %ld", [words count]);
for(NSString *name in names) {
// Pick a proper name then search for it in the word list
for(NSString *word in words) {
// if found, print it out
if([name caseInsensitiveCompare:word] == NSOrderedSame ) {
// NSLog(@"Name: %@ Word: %@", name, word);
// Test only words that are not proper names
if(![name isEqualToString:word]) {
// must be different by a capital letter. ie. "Glen" to "glen"
NSLog(@"Name: %@ Word: %@", name, word);
// Stop loop early, because we found the match
// this will run a couple seconds faster.
break;
}
}
}
}
NSLog(@"Done");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment