Created
May 6, 2013 02:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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