Skip to content

Commit f78b1fd

Browse files
authored
Merge pull request #50 from clalexni/main
fix reverse.c
2 parents 5fd36ec + dfb993f commit f78b1fd

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

chapter_1/exercise_1_19/reverse.c

Lines changed: 13 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 max_line_len 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,15 @@ void reverse(char line[])
5862
int i_back = length(line);
5963
char temp;
6064

61-
i_back -= 2;
65+
if(line[i_back - 1] == '\n')
66+
{
67+
i_back -= 2;
68+
}
69+
else
70+
{
71+
i_back -= 1;
72+
}
73+
6274
while (i_back > i_front)
6375
{
6476
temp = line[i_front];

0 commit comments

Comments
 (0)