#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// read in propernames file as a huge string(ignoring possibility of errors
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding error:NULL];
// read in words file as a huge string(ignoring possibility of errors
NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding error:NULL];
// break proper names into an array of strings
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
// break word into an array of strings
NSArray *words = [wordString componentsSeparatedByString:@"\n"];
int currentNamePosition; //stores current position in proper name array
int currentWordPosition; //stores current position in common word array
// go through every word in the proper name array
for (currentNamePosition = 0; currentNamePosition < [names count]; currentNamePosition++)
{
//go through every word in the common word array
for (currentWordPosition = 0; currentWordPosition < [words count]; currentWordPosition++)
{
//if current word in proper name array case insensitive matches current word in common word array
if ([names[currentNamePosition] caseInsensitiveCompare:words[currentWordPosition]] == NSOrderedSame)
{
//only display the proper name and lowercase match
if (!([names[currentNamePosition]isEqualToString:words[currentWordPosition]]))
{
NSLog(@"%@\n", names[currentNamePosition]);
NSLog(@"%@\n", words[currentWordPosition]);
}
}
}
}
}
return 0;
}
I have to learn to be more elegant with my code.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Read a file as a huge string (ignore possibility of error)
NSString *wordList = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
//break into an array of word strings
NSArray *words = [wordList componentsSeparatedByString:@"\n"];
// read name file
NSString *nameList = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding
error:NULL];
// build the array of name strings
NSArray *names = [nameList componentsSeparatedByString:@"\n"];
for (NSString *name in names)
{
for (NSString *word in words)
{
NSComparisonResult nameIsWord = [[name lowercaseString] compare:word];
if(nameIsWord == NSOrderedSame)
{
NSLog(@"%@ is in the name list and in the word list", name);
break; // no need to continue searching for a match
}
}
}
}
return 0;
}
A similar solution to yours but I used fast enumeration. I like your nesting of the NSString:Compare in the conditional.
I also went for the quick enumeration mode, with another inside, but I don’t appear to be working correctly. It finds A and a but then seems to hang - Its also a really big word list I’d imagine, and the recursive send through might just take a while?
This was my solution using fast enumeration too, Thanks for sharing to compare approaches for the challenge.
[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//Read in a file 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];
//Break it into an Array of strings
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
NSArray *words = [wordString componentsSeparatedByString:@"\n"];
//Iterate with fast enumeration, getting with nested loops the names and words
for (NSString *catchName in names) {
for (NSString *catchWord in words) {
//Comparing both strings and printing.
if ([catchName isEqualToString:catchWord]) {
NSLog(@"%@ in names is the same as %@ in words",catchName,catchWord);
}
}
}
}
return 0;
}
[/code]