/
ex1-19.c
54 lines (45 loc) · 968 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
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
int getaline(char line[], int maxline);
void reverse(char s[]);
/* Remove trailing blanks and tabs. Omit empty lines. */
main()
{
int len; /* current line length */
char line[MAXLINE]; /* current input line */
while ((len = getaline(line, MAXLINE)) > 0) {
line[len-1] = '\0'; // chop off the newline
reverse(line);
printf("%s\n", line);
}
return 0;
}
/* reverse: reverses string s */
void reverse(char s[])
{
int i, j;
char tmp;
/* find the end of the string */
for (j=0; s[j]!='\0'; ++j)
;
--j; // j points at the last char in the string
for (i=0; j>i; ++i) {
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
--j;
}
}
/* getaline: read a line into s, return length */
int getaline(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;
}