ios - 一道OC练习题
PHPz
PHPz 2017-04-17 13:59:05
[iOS讨论组]

/usr/share/dict/propernames 文件保存着常见专有名词。/usr/share/dict/words 文件保存着常用的单词(不包含专有名词),请找出既是常见专有名词又是常用单词的字符串,例如Glen是男名,glen指狭长的山谷。

求解答,谢谢。

PHPz
PHPz

学习是最好的投资!

全部回复(3)
ringa_lee
#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循环遍历两个数组进行比较,有相同的就放到一个新的可变数组里

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号