-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex1_19.c
59 lines (49 loc) · 958 Bytes
/
ex1_19.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
int getline1(char line[], int lim);
void reverse(char s[]);
int main()
{
char line[1000];
while (getline1(line, 1000) > 0) {
reverse(line);
printf("%s\n", line);
}
return 0;
}
int getline1(char line[], int lim)
{
int c, i, j;
i = j = 0;
while ((c = getchar()) != EOF && (c != '\n')) {
if (i < lim - 1)
line[j++] = c;
++i;
}
if (c == '\n') {
if (i <= lim - 1)
line[j++] = c;
++i;
}
line[j] = '\0';
return i;
}
void reverse(char s[])
{
int i, j;
char temp;
//finds the length of the character array
for (i = 0; s[i] != '\0'; ++i)
;
--i;
//removes newline character if present
if (s[i] == '\n') {
s[i] = '\0';
--i;
}
for (j = 0; j < i; j++) {
temp = s[j];
s[j] = s[i];
s[i] = temp;
--i;
}
}