Challenge 1:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *listOfNames = @"AdamIsabellePatJohnLeo"; // A long list of names
NSString *name = @"asd";
NSRange match = [listOfNames rangeOfString:name
options:NSCaseInsensitiveSearch];
if (match.location == NSNotFound) {
NSLog(@"\nNo match found!");
} else {
NSString *nameFound = [listOfNames substringWithRange:match];
NSLog(@"\nMatch found! \"%@\" is the portion of the string that was found", nameFound);
}
}
return 0;
}
Challenge 2:
#import <Foundation/Foundation.h>
#import <readline/readline.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"\nWhere should I start counting? ");
const char *number = readline(NULL);
NSString *nsstringNumber = [NSString stringWithUTF8String:number];
NSInteger z = [nsstringNumber integerValue];
NSInteger i = 0;
for (i = z; i >= 0; i -= 3) {
if (i % 5 == 0) {
NSLog(@"\n%ld - Found one!", (long)i);
} else {
NSLog(@"\n%ld", (long)i);
}
}
}
return 0;
}
In hindsight, using nsstringNumber as a name was a mistake as it requires a LOT more typing to filter down to the required item. This wasn’t a massive deal with a short program like this, but for larger programs, it could become tiresome and potentially somewhere that could cause some troubleshooting headaches.
Any suggestions for making the code more streamlined and efficient would be appreciated.
This was a good challenge - the first one that stumped me for a while, but I think that was partly due to lack of sleep/new baby.