From 3cf6d7a97591ee0902eb70d29b6c2119cec35c57 Mon Sep 17 00:00:00 2001 From: Ganesh Kamath Date: Sun, 16 Jan 2022 22:14:07 +0530 Subject: [PATCH] Create FillingJars.c https://www.hackerrank.com/challenges/filling-jars/problem --- FillingJars.c | 197 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 FillingJars.c diff --git a/FillingJars.c b/FillingJars.c new file mode 100644 index 0000000..d9e1af5 --- /dev/null +++ b/FillingJars.c @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char* readline(); +char* ltrim(char*); +char* rtrim(char*); +char** split_string(char*); + +int parse_int(char*); + +/* + * Complete the 'solve' function below. + * + * The function is expected to return an INTEGER. + * The function accepts following parameters: + * 1. INTEGER n + * 2. 2D_INTEGER_ARRAY operations + */ + +long solve(long n, long operations_rows, int operations_columns, long** operations) { + + long *arr = (long *) malloc (n*sizeof(long)); + memset(arr, 0, n); + long sum = 0; + for (long i = 0; i= str && isspace(*end)) { + end--; + } + + *(end + 1) = '\0'; + + return str; +} + +char** split_string(char* str) { + char** splits = NULL; + char* token = strtok(str, " "); + + int spaces = 0; + + while (token) { + splits = realloc(splits, sizeof(char*) * ++spaces); + + if (!splits) { + return splits; + } + + splits[spaces - 1] = token; + + token = strtok(NULL, " "); + } + + return splits; +} + +int parse_int(char* str) { + char* endptr; + int value = strtol(str, &endptr, 10); + + if (endptr == str || *endptr != '\0') { + exit(EXIT_FAILURE); + } + + return value; +}