Pages

Sunday, June 16, 2013

coding help

For one of the tutorials it asks me to compare the two files words and propernames. I have to list out the matching words from both files.

 

#import

 

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"];

       

        //Go through the array one string at a time

        for(NSString *n in names){

            for(NSString *w in words){

               if ([n caseInsensitiveCompare:w] == NSOrderedSame) {

                    NSLog(@"The words %@ and %@ matches", n, w);

                }

            }

        }

     }

    return 0;

}

 

This is how I approach it however it didn't work as I planned it. Anyone can help with this?


View the original article here

0 comments:

Post a Comment