-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_line.c
More file actions
48 lines (41 loc) · 848 Bytes
/
Copy pathreverse_line.c
File metadata and controls
48 lines (41 loc) · 848 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
#include <stdio.h>
#define MAXLINE 100
int get_line(char line[], int maxline);
void reverse(char line[], char reversed_line[]);
// Print longest line
int main(){
int len;
int max;
char line[MAXLINE];
char reversed_line[MAXLINE];
max = 0;
while(get_line(line, MAXLINE) > 0){
reverse(line, reversed_line);
printf("%s\n", reversed_line);
}
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 line[], char reversed_line[]){
int i, j;
j = 0;
while (line[j] != '\0')
j++;
j--;
i = 0;
while(j > -1){
reversed_line[i] = line[j];
i++;
j--;
}
}