-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-19.c
80 lines (61 loc) · 1.92 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
70
71
72
73
74
75
76
77
78
79
/*
Write a function reverse (s) that reverses the character strings.
Use it to write programme which reverse the charactor string s
in input a line at a time
*/
#include <stdio.h>
#define MAXLINE 20 /* maximum input of size */
#define CHARNUM 1 /* Character Number - get at least 2 charactor */
int getLine(char line[], int maxline);
void reverse(char to[], char from[], int j);
/* print all input lines that are longer than 80 characters */
int main(){
int len; /* line length by getchar */
char line[MAXLINE]; /* current input line - string */
char reverseLines[MAXLINE]; /* lappended input line */
int j;
j = 0;
printf ("This will print out any input lines more than %d \n", CHARNUM);
while ((len = getLine (line, MAXLINE)) > 0)
{
if (len > CHARNUM ){
reverse (reverseLines, line, len);
}
printf("%s \n", reverseLines);
}
--j;
reverseLines[j] = '\0';
printf("%s \n", reverseLines);
return 0;
}
/*
getLine: read a line return int - the length of input
(changed the name from getline as stdio.h has a declared func with the same name)
arg[0]: char array - input text
arg[1]: int - the maimum limit of the length of input text (arg [0])
*/
int getLine(char a[], int lim){
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++){
a[i] = c;
}
// once excuted it will be added as ++i so no need to add extra
a[i] = '\0';
return i;
}
/*
reverse: get ricerse order of a string
arg[0]: char array - reversed text
arg[1]: char array - original text
arg[2]: int - length of arg[1]
*/
void reverse (char to[], char from[], int j){
int i;
i = 0;
to[j] = '\0';
while (j != 0){
j--;
to[j] = from[i];
i++;
}
}