Using readline() challenge


#1

Here’s my take on this challenge. I’ve used scanf() instead of readline() as I couldn’t get the latter to work.

[code]//
// main.m
// UsingReadline
//
// Created by Antony Kwok on 22/10/2014.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//

/*** Previous code challenge in C ***/

//int counter = 0;
//printf(“Where should I start counting? “);
//const char *userInput = readline(NULL);
//for (int i = atoi(userInput); i >= 0; i–) {
// if (counter == 2) {
// counter = 0;
// continue;
// }
// printf(”%d\n”, i);
// if (i % 5 == 0) {
// printf(“Found one!\n”);
// }
// counter++;
//}

/*** Updated code challenge in Objective-C ***/

#import <Foundation/Foundation.h>

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

    char cString[20];
    NSUInteger counter = 0; // used to skip every third number
    NSLog(@"Where should I start counting? ");
    
    // scanf takes 19 characters max (not 20) because it's terminated with a NULL character
    scanf("%19s", cString);
    
    NSString *userInput = [NSString stringWithUTF8String:cString];
    
    for (NSInteger i = [userInput integerValue]; i>=0; i--) {
        if (counter == 2) {
            counter = 0;
            continue;
        }
        NSLog(@"%ld", (long)i);
        if (i % 5 == 0) {
            NSLog(@"Found one!");
        }
        counter++;
    }
    
}
return 0;

}
[/code]