-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_reverse.c
More file actions
50 lines (40 loc) · 805 Bytes
/
function_reverse.c
File metadata and controls
50 lines (40 loc) · 805 Bytes
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
#include <stdio.h>
#define MAXLINE 1000 /*maximum input line size */
int get_line(char line[], int maxline);
void reverse(char out[], char in[], int maxline);
/* print the reverse of a line */
main()
{
int len;
char line[MAXLINE];
char rev[MAXLINE];
while((len=get_line(line, MAXLINE))>0){
reverse(rev, line, len);
printf("%s", rev);
}
return 0;
}
int get_line(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;
}
void reverse(char to[], char from[], int len){
int i;
int k;
if(from[len-1]=='\n'){
to[len-1]='\n';
k=2;
}else{
k=1;
}
for(i=len-k; i>=0; --i)
to[len-k-i]=from[i];
to[len]='\0';
}