#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// (1) Read in propernames file as a huge string
// convert *namesList to lowercaseString so that I can find the matched words
NSString *namesList = [[NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding
error:NULL] lowercaseString];
// (2) Read in words file as a huge string
NSString *wordsList = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
// (3) Break them into arrays of strings
NSArray *names = [namesList componentsSeparatedByString:@"\n"];
NSArray *words = [wordsList componentsSeparatedByString:@"\n"];
// (4) Use 'fast enumeration' to check the duplicates
for (NSString *n in names ) {
for (NSString *i in words) {
// Look for duplicates
if ([n isEqualToString:i] == YES) {
// Print them if found
NSLog(@"\n%@ is a duplicate.\n", i);
}
}
}
return 0;
}
}
文件内容是什么格式的数据?
从文件读取出数据放到数据,然后两个for循环遍历两个数组进行比较,有相同的就放到一个新的可变数组里