/*Copyright ElohimTov LLC*/ /* //Name: ElohimTov Solve Code - Data Types - Objective-C Solution //Problem: HackerRank - 30 Days of Code - Data Types //Current File: ElohimTovSolve_DataTypes.(objc).txt //Compile-able File: ElohimTovSolve_DataTypes.objc //Dates: (Created 02/20/2021); 05/17/2021; 12/31/2021. //Tale: ElohimTov Solve's Solution to HackerRank.com's // Data Types problem in Objective-C programming language. // Problem link at https://elohimtov.com/elohimtov.info. */ /****NOTE: TutorialsPoint.com Coding Ground was used to Code, Write, Modify, Execute, Test and Solve this Objective-C problem. https://www.tutorialspoint.com/compile_objective-c_online.php****/ //Objective-C code begins #import #import #import @interface NSString (NumberFromString) - (NSNumber *) numberFromString:(NSNumberFormatter *)formatter; @end @implementation NSString (NumberFromString) - (NSNumber *) numberFromString:(NSNumberFormatter *)formatter { NSNumber *number = [formatter numberFromString:self]; if (number == nil) { [NSException raise:@"Bad Input" format:@"%@", self]; } return number; } @end int main(int argc, const char* argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; NSData *availableInputData = [[NSFileHandle fileHandleWithStandardInput] availableData]; NSString *availableInputString = [[NSString alloc] initWithData:availableInputData encoding:NSUTF8StringEncoding]; NSArray *availableInputArray = [availableInputString componentsSeparatedByString:@"\n"]; NSUInteger currentInputLine = 0; //do the integer addition and print answer NSNumber *i = [NSNumber numberWithInt:4]; NSNumber *integer_input = [[availableInputArray objectAtIndex:currentInputLine] numberFromString:numberFormatter]; currentInputLine += 1; int integer_answer = [i intValue] + [integer_input intValue]; //do the double addition and print result NSNumber *d = [NSNumber numberWithDouble:4.0]; NSNumber *double_input = [[availableInputArray objectAtIndex:currentInputLine] numberFromString:numberFormatter]; currentInputLine += 1; double double_answer = [d doubleValue] + [double_input doubleValue]; //do the string concatenation and print final string. //create and initiate NSStrings NSString *str = @"HackerRank "; NSString *str0 = [availableInputArray objectAtIndex:currentInputLine]; //convert NSStrings to chars strings const char *char_str = [str UTF8String]; const char *char_str0 = [str0 UTF8String]; currentInputLine += 1; //print integer summation answer printf("%i\n", integer_answer); //print double summation answer printf("%.1f\n", double_answer); //print char Strings simultaneously below using printf printf("%s%s", char_str, char_str0); [pool drain]; return 0; }//ends main //Objective-C code ends /*Copyright ElohimTov LLC*/