Can you please check my second challenge solution?


#1
#import <Foundation/Foundation.h>

int testOne(int one)
{
    NSLog(@"\nFirst File");
    
    NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                                     encoding:NSUTF8StringEncoding
                                                        error:NULL];
    NSArray *names = [nameString componentsSeparatedByString:@"\n"];
    int test = 0;
    for (NSString *n in names) {
        NSRange r = [n rangeOfString:@"Wolf" options:NSLiteralSearch];
        if (r.location != NSNotFound) {
            NSLog(@"\nFound, %@", n);
            test++;
        }
    }
    return test;
}

int testTwo(int two)
{
    NSLog(@"\nSecond File");
    
    NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                     encoding:NSUTF8StringEncoding
                                                        error:NULL];
    NSArray *names = [nameString componentsSeparatedByString:@"\n"];
    int test = 0;
    for (NSString *n in names) {
        NSRange r = [n rangeOfString:@"Wolf" options:NSLiteralSearch];
        if (r.location != NSNotFound) {
            NSLog(@"\nFound, %@", n);
            test++;
        }
    }
    return test;
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        int t1;
        int t2;
        int tt1 = testOne(t1);
        int tt2 = testTwo(t2);
        if ((tt1) && (tt2)) {
            NSLog(@"they share some content!");
        }
        
    }
    return 0;
}

#2

Good, but you need only one function.

[code]#import <Foundation/Foundation.h>

int searchStringInFile (NSString * string, NSString * path)
{
NSLog (@“File: %@”, path);

NSString *nameString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding
                                                    error:NULL];
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
int count = 0;
for (NSString *n in names) {
    NSRange r = [n rangeOfString:string options:NSLiteralSearch];
    if (r.location != NSNotFound) {
        NSLog(@"\nFound, %@", n);
        count++;
    }
}
return count;

}

int main (int argc, const char * argv[])
{
@autoreleasepool {

    NSString *const file1 = @"/usr/share/dict/words";
    NSString *const file2 = @"/usr/share/dict/propernames";
    NSString *const word  = @"Wolf";
    
    int rv1 = searchStringInFile (word, file1);
    int rv2 = searchStringInFile (word, file2);
    if ((rv1) && (rv2)) {
        NSLog (@"they share some content!");
    }
    
}
return 0;

}[/code]


#3

[quote=“ibex10”]Good, but you need only one function.

[code]#import <Foundation/Foundation.h>

int searchStringInFile (NSString * string, NSString * path)
{
NSLog (@“File: %@”, path);

NSString *nameString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding
                                                    error:NULL];
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
int count = 0;
for (NSString *n in names) {
    NSRange r = [n rangeOfString:string options:NSLiteralSearch];
    if (r.location != NSNotFound) {
        NSLog(@"\nFound, %@", n);
        count++;
    }
}
return count;

}

int main (int argc, const char * argv[])
{
@autoreleasepool {

    NSString *const file1 = @"/usr/share/dict/words";
    NSString *const file2 = @"/usr/share/dict/propernames";
    NSString *const word  = @"Wolf";
    
    int rv1 = searchStringInFile (word, file1);
    int rv2 = searchStringInFile (word, file2);
    if ((rv1) && (rv2)) {
        NSLog (@"they share some content!");
    }
    
}
return 0;

}[/code][/quote]

Yes it’s way shorter, Thanks for making it clearer.
but there is something am not sure of, the NSLiteralSearch it is not really literal, if am searching for Wolf only it’s giving me Wolfgang too for example is that literal search ?


#4

To find out if the match found a proper word (a word that is not a substring):

NSRange r = [n rangeOfString:string options:NSLiteralSearch];

You can inspect the values inside the returned NSRange object. For a proper word match, the value of location will be zero and the value of length will be the length of the word being searched for.

[Become a competent programmer faster than you can imagine: pretty-function.org]


#5

[quote=“ibex10”]To find out if the match found a proper word (a word that is not a substring):

NSRange r = [n rangeOfString:string options:NSLiteralSearch];

You can inspect the values inside the returned NSRange object. For a proper word match, the value of location will be zero and the value of length will be the length of the word being searched for.

[Become a competent programmer faster than you can imagine: pretty-function.org][/quote]

You mean this way:
if ((r.location == 0) && (r.length == 4));
??


#6

Yes:

if ((r.location == 0) && (r.length == [string length])) {
   ...
}