I’m trying to figure out where I went wrong. I get 1602 matches and items in my result array are duplicated. Can someone help me with this?
Thank you very much.
[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Read in a file as a huge string (ingoring possible errors)
NSString *nameString = [[NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL] lowercaseString];
NSString *wordString = [[NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL] lowercaseString];
// Break it into an array of strings
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
NSArray *words = [wordString componentsSeparatedByString:@"\n"];
// results will be put here
NSMutableArray *results = [[NSMutableArray alloc] init];
for(NSString *name in names) {
for(NSString *word in words) {
if (([name length] == [word length]) && [name isEqualToString:word] == YES){
[results addObject:word];
}
}
}
// print it
for (NSString *result in results) {
NSLog(@"%@", result);
}
NSLog(@"\nDONE");
NSLog(@"matches: %lu", [results count]);
}
return 0;
}[/code]