Challenge solution for 2nd Edition

What is the Challenge solution for the Chapter 16: NSString in the 2nd edition?

Is this right:

{
NSString *name= [NSString stringWithUTF8Sring:@"Dave"];
NSLog(@"Who is cool? %@ is cool!", name);
return 0;
}

My solution :

{
        NSLog(@"Who is cool ? ");
        const char *name = readline(NULL);
        NSString *aName = [NSString stringWithUTF8String:name];
        NSLog(@"%@ is cool", aName);
}

Don’t forget to import the <readline/readline.h> library so you can use the readline() function. Also, if you are getting build failures, remember you need to go to the ‘Build Phases’ section and add ‘libreadline.dylib’ in the ‘Link Binary With Libraries’ section. If you forgot how to do that, just flip back and read the entire challenge section for Chapter 8 that explains how to use readline().

My solution:

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

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

    NSLog(@"Who is cool? ");
    NSString *name = [NSString stringWithUTF8String:readline(NULL)];
    NSLog(@"%@ is cool!", name);
}

return 0;

}[/code]

Also, Chapter 16 (2nd edition) technically has two challenges, so here is my solution for the first challenge that has to do with using the rangeOfString method to match a substring that is not case-sensitive:

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

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

    NSString *listOfNames = @"John, Billy, Bob, Randy, Charles, Tom, Greg";
    NSString      *search = @"greg";
    NSRange        match1 = [listOfNames rangeOfString:search];
    NSRange        match2 = [listOfNames rangeOfString:search options:NSCaseInsensitiveSearch];
    
    NSLog(@"Searching \"%@\" for \"%@\"", listOfNames, search);
    
    if (match1.location == NSNotFound) {
        NSLog(@"A case-sensitive match was NOT found!");
    } else {
        NSLog(@"A case-sensitive match was found!");
        NSLog(@"And here it is! '%@'", [listOfNames substringWithRange:match1]);
    }
    
    if (match2.location == NSNotFound) {
        NSLog(@"A case-insensitive match was NOT found!");
    } else {
        NSLog(@"A case-insensitive match was found!");
        NSLog(@"And here it is! '%@'", [listOfNames substringWithRange:match2]);
    }
}

return 0;

}[/code]

[quote=“lephannam”]My solution :

{ NSLog(@"Who is cool ? "); const char *name = readline(NULL); NSString *aName = [NSString stringWithUTF8String:name]; NSLog(@"%@ is cool", aName); } [/quote]

Mine differed a little given that that the topic had us use stringWithUTF8String instead of const char *:

{

     NSLog(@"Who is cool?  ");
     NSString *answer = [NSString stringWithUTF8String:readline(NULL)];
     NSLog(@"%@ is cool.", answer);
}

This is my solution, but why does the for work?

[code]#import <Foundation/Foundation.h>
#import <readline/readline.h>
#import <stdio.h>
#import <stdlib.h>

int main(int argc, const char * argv[])
{
//Get the string form the user
NSLog(@"Where should I start countting? ");

//Convert to NSString
NSString *number = [NSString  stringWithUTF8String:readline(NULL)];
int num = [number intValue];
//Log Result
NSLog(@"You entered: %i", num);

for ([number intValue]; num >= 0; num -= 3) {
    NSLog(@"%i\n", num);
    if (num % 5 == 0) {
        NSLog(@"Found One!\n");
    }
}

}[/code]

I expected this to work :for (num; num >= 0; num -= 3) { NSLog(@"%i\n", num); if (num % 5 == 0) { NSLog(@"Found One!\n"); } } but it doesn’t?

[quote]I expected this to work :

for (num; num >= 0; num -= 3) {
        NSLog(@"%i\n", num);
        if (num % 5 == 0) {
            NSLog(@"Found One!\n");
        }
 }

but it doesn’t?
[/quote]

[quote]for ([color=#FF0000]num[/color]; num >= 0; num -= 3) {

}[/quote]
What’s [color=#FF0000]num[/color] for?

Maybe you wanted to write it like this:

for (; num >= 0; num -= 3) {
        NSLog(@"%i\n", num);
        if (num % 5 == 0) {
            NSLog(@"Found One!\n");
        }
 }

For the first part using rangeOfString, I combined the two:

#import <Foundation/Foundation.h>
#import <readline/readline.h>

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

    @autoreleasepool {
        

        NSLog(@"\n\nPlease provide a name to search for (case-insensitive):  ");
        NSString *find = [NSString stringWithUTF8String:readline(NULL)];
        
        NSString *listOfNames = @"Billy, Bob, Brian, Greg, Thomas, Phillip";
        NSRange match = [listOfNames rangeOfString:find options:NSCaseInsensitiveSearch];
        
        
        if (match.location == NSNotFound) {
            NSLog(@"No match found!");
        } else {
            NSLog(@"\nMatch found!");
            NSLog(@"\nYou searched for: %@", find);
            NSLog(@"\n Text Found: %@", [listOfNames substringWithRange:match]);
        }
    }
    return 0;
}