Skip to content

Commit 77b2330

Browse files
authored
fix reverse.c
1 parent 5fd36ec commit 77b2330

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

chapter_1/exercise_1_19/reverse.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ int get_line(char line[], int max_line_len)
3131
++i;
3232
}
3333

34+
// flush out input stream if exceeding MAXLINE limit
35+
while (i >= max_line_len - 1 && (c = getchar()) != '\n')
36+
;
37+
3438
if (c == '\n')
3539
{
3640
line[i] = '\n';
@@ -58,7 +62,7 @@ void reverse(char line[])
5862
int i_back = length(line);
5963
char temp;
6064

61-
i_back -= 2;
65+
i_back = line[i_back - 1] == '\n'? i_back - 2: i_back - 1;
6266
while (i_back > i_front)
6367
{
6468
temp = line[i_front];

0 commit comments

Comments
 (0)