A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Even Odd in Objective-C page! Here, you'll find the source code for this program as well as a description of how the program works.
#import <Foundation/Foundation.h>
@interface NSString (Parsing)
@property(nonatomic, readonly, nullable) NSNumber* strictIntegerNumber;
@end
@implementation NSString (Parsing)
- (NSNumber*)strictIntegerNumber {
NSString* trimmed = [self
stringByTrimmingCharactersInSet:NSCharacterSet
.whitespaceAndNewlineCharacterSet];
if (trimmed.length == 0) return nil;
NSInteger offset = 0;
if ([trimmed hasPrefix:@"-"] || [trimmed hasPrefix:@"+"]) {
offset = 1;
}
if (trimmed.length <= offset) return nil;
NSString* core = [trimmed substringFromIndex:offset];
NSRange invalidRange =
[core rangeOfCharacterFromSet:NSCharacterSet.decimalDigitCharacterSet
.invertedSet];
if (invalidRange.location != NSNotFound) {
return nil;
}
return @(trimmed.longLongValue);
}
@end
int main(int argc, const char* argv[]) {
@autoreleasepool {
if (argc < 2) {
puts("Usage: please input a number");
return 1;
}
NSNumber* number = @(argv[1]).strictIntegerNumber;
if (!number) {
puts("Usage: please input a number");
return 1;
}
puts(number.longLongValue % 2 == 0 ? "Even" : "Odd");
}
return 0;
}
Even Odd in Objective-C was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.