I have a question about my little c program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int c, len;
int max = 100;
char *buffer = malloc(max);
for (len = 0; (c = getchar()) != EOF; len++) {
buffer[len] = c;
if (len == max - 1) {
buffer = realloc(buffer, (len + max));
if (buffer == NULL) {
printf("Error: Out of memory!\n");
return 1;
}
max += 100;
}
}
buffer[len] = '\0';
for (; len >= 0; --len) {
printf("%c", buffer[len]);
}
printf("\n");
free(buffer);
return 0;
}
My task is to write a program which inserts a text and gives a backwards output of the text. If there happens to be a problem with the allocated memory an error message should occur. According to my test report from university the first lines of the output are 1 character too long, I can't determine the reason for this problem and I'm seeking for some advice and help