(Input--):Test
(Output): (60 character wide blank space) 6
/* Title: Exercise_1_16 ** Description: A program that prints the length of strings and as much text as the buffer will allow.*/ #include <stdio.h> #define ARRAY_SIZE 1024 int mygetline(char *line_input, int buffer_size); main() { int strlen; char line[ARRAY_SIZE]; while ((strlen = mygetline(line, ARRAY_SIZE)) > 0) { printf("%60s\t%d\n", line, strlen); } } int mygetline(char *line_input, int buffer_size) { int c, i; for (i = 1; i < buffer_size && (c = getchar()) != EOF && c != '\n'; i++) { line_input[i] = c; } if (c == '\n') { line_input[i] = c; i++; } line_input[i] = '\0'; return i; }
Edit - I went through my code a few more times, and I found that it was an off by one error. Because the for loop in the mygetline function was starting at i=1, it was printing at [1] in the array and leaving a \0 at position [0].
Edited by Wolvendeer, 05 March 2012 - 12:50 PM.
Solved