-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-21.c
107 lines (88 loc) · 2.28 KB
/
1-21.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Create entab programe - replace 4 white space in the input to a tub
*/
#include <stdio.h>
#include <stdbool.h>
#define MAXNUMBER 100 // Max number of input
#define WS 4 // white space
int getLine(char input[], int lim);
bool entab(char input[], int c);
void copyAppend(char to[], char from[], int i);
int main(){
char line[MAXNUMBER]; // current input line - string
char allLines[MAXNUMBER]; // lappended input line
int len; // line length by getchar
int j;
j = 0;
while ((len = getLine (line, MAXNUMBER)) > 0)
{
copyAppend (allLines, line, j);
// donot overwrite '\n'
j = j + len + 1;
}
// overwrite '\n'
--j;
printf ("The last index is %d \n", j);
allLines[j] = '\0';
printf("%s \n", allLines);
return 0;
}
/*
entub: switch tub to number of white spaces
arg[0]: char array - input text
arg[1]: current index number
*/
bool entub (char input[], int i){
int j;
j=1;
while (j < WS && input[i-j] == ' '){
j++;
if (j == WS){
return true;
}
}
return false;
}
/*
getLine: read a line return int - the length
arg[0]: char array - input text
arg[1]: int - the maimum limit of the length of input text (arg [0])
*/
int getLine(char input[], int lim){
int i, k;
// getchar up till eof or newline
for (i = 0; i < lim - 1 && (k = getchar()) != EOF && k != '\n'; i++){
// when tab detected
if (k == ' '){
// changer tab to
if (entub (input, i)){
i = i - WS + 1;
input[i] = '\t';
}
else {
input[i] = k;
}
}
// otherwise just assign
else {
input[i] = k;
}
}
input [i] = '\0';
return i;
}
/*
copyAppend: read a line append onto
arg[0]: char array - where appending to
arg[1]: char array - to get appended onto arg[0]
arg[2]: int - index of arg[0] where appending from
*/
void copyAppend (char to[], char from[], int j){
int i;
i = 0;
while ((to[j] = from[i]) != '\0'){
++i;
++j;
}
to[j] = '\n';
}