-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetab.c
More file actions
54 lines (51 loc) · 1.75 KB
/
detab.c
File metadata and controls
54 lines (51 loc) · 1.75 KB
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 TABSTOP 8
int main()
{
int chr, spaceCalc, i;
int pos = 0;
/* gets each character and checks if it is the EOF character or not*/
while((chr = getchar()) != EOF)
{
/* if character is tab */
if(chr == '\t')
{
/* calculate the number of spaces to add*/
spaceCalc = TABSTOP - (pos % TABSTOP);
for(i = 0; i < spaceCalc; i++)
{
/*print space character and increment pos*/
putchar(' ');
pos++;
}
}
/* if characters are newline or carriage return*/
else if (chr == '\n' || chr == '\r')
{
/* print specific character, reset pos to 0*/
putchar(chr);
pos = 0;
}
/* if character is backspace*/
else if (chr == '\b')
{
/* decrement pos, check if pos is on left margin
(if pos is before left margin, reset pos to 0),
print specific character. */
pos--;
if(pos < 0)
{
pos = 0;
}
putchar(chr);
}
/* any other character*/
else
{
putchar(chr);
pos++;
}
}
/* return 0 on success*/
return 0;
}