Question
#include #include #include #include #include #include #include #include #include #include char* readline(); char* ltrim(char*); char* rtrim(char*); int parse_int(char*); /* * Complete the 'combineParts' function below.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
char* readline();
char* ltrim(char*);
char* rtrim(char*);
int parse_int(char*);
/*
* Complete the 'combineParts' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY parts as parameter.
*/
int combineParts(int parts_count, int* parts)
{
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
int parts_count = parse_int(ltrim(rtrim(readline())));
int* parts = malloc(parts_count * sizeof(int));
for (int i = 0; i < parts_count; i++) {
int parts_item = parse_int(ltrim(rtrim(readline())));
*(parts + i) = parts_item;
}
int result = combineParts(parts_count, parts);
fprintf(fptr, "%d ", result);
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) {
break;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == ' ') {
break;
}
alloc_length <<= 1;
data = realloc(data, alloc_length);
if (!data) {
data = '\0';
break;
}
}
if (data[data_length - 1] == ' ') {
data[data_length - 1] = '\0';
data = realloc(data, data_length);
if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);
if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}
return data;
}
char* ltrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
while (*str != '\0' && isspace(*str)) {
str++;
}
return str;
}
char* rtrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
char* end = str + strlen(str) - 1;
while (end >= str && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
return str;
}
int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);
if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}
return value;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started