-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_19.c
69 lines (56 loc) · 1.2 KB
/
1_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
60
61
62
63
64
65
66
67
68
69
/*
* Exercise 1.19
*
* Write a function reverse(s) that reverses the character string s.
* Use it to write a program that reverses its input a line at a time.
*
*/
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
void reverse(char line[]);
main()
{
char line[MAXLINE];
while ((getline(line, MAXLINE)) > 0) {
reverse(line);
printf("%s",line);
}
return 0;
}
void reverse(char s[])
{
int length;
int c, i, j;
for (length = 0; s[length] != '\0'; ++length)
;
char r[length];
for(i = length-2, j = 0; i >=0; --i, ++j) {
r[j] = s[i];
}
r[length-1] = s[length-1];
r[length] = '\0';
copy(s,r);
}
/* getline: read a line into s, return length */
int getline(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}