solutions/The C Programming Language/1.20.c

75 lines
1.0 KiB
C
Raw Normal View History

2024-05-02 06:54:04 -04:00
#include <stdio.h>
// replacing tabs with spaces so it keeps indentation
2024-05-02 06:54:04 -04:00
int _getline(char s[], int lim)
{
int c, i, j;
for (i = 0; (c = getchar()) != '\n'; ++i)
{
if (i < lim - 1)
s[i] = c;
}
j = i;
if (j > lim - 1)
j = lim - 1;
s[j] = '\0';
return j - 1;
}
void entab(char to[], char from[], int columns)
{
int is_placing_spaces = 0;
int remaining_spaces = columns;
int i_to = 0;
int i_from = 0;
while (from[i_from] != '\0')
{
if (from[i_from] == '\t')
is_placing_spaces = 1;
if (is_placing_spaces)
{
if (remaining_spaces == 0)
{
is_placing_spaces = 0;
remaining_spaces = columns + 1;
++i_from;
}
else
{
to[i_to] = '_';
++i_to;
}
}
else
{
if (remaining_spaces == 0)
remaining_spaces = columns;
to[i_to] = from[i_from];
++i_to;
++i_from;
}
--remaining_spaces;
}
to[i_to] = '\0';
}
main()
{
int size = 500;
char from[size];
char to[size];
while (1)
{
_getline(from, size);
entab(to, from, 4);
printf("%s\n", to);
}
}