Created
June 20, 2012 15:41
-
-
Save yitznewton/2960563 to your computer and use it in GitHub Desktop.
K&R Exercise 1-20: detab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define MAXLINE 1000 | |
#define TABWIDTH 4 | |
int slurp_into(char input[]); | |
void output_detabbed(char input[], int length); | |
void output_spaces(int how_many); | |
int main() | |
{ | |
char input[MAXLINE]; | |
int length; | |
length = slurp_into(input); | |
output_detabbed(input, length); | |
return 0; | |
} | |
int slurp_into(char input[]) | |
{ | |
int i; | |
int c; | |
for (i = 0; i < MAXLINE-1 && (c = getchar()) != EOF; ++i) { | |
input[i] = c; | |
} | |
return i; | |
} | |
void output_detabbed(char input[], int length) | |
{ | |
int i; | |
int line_position = 0; | |
for (i = 0; i < length; ++i) { | |
if (input[i] == '\n') { | |
putchar(input[i]); | |
line_position = 0; | |
} | |
else if (input[i] == '\t') { | |
output_spaces(TABWIDTH - (line_position % TABWIDTH)); | |
} | |
else { | |
putchar(input[i]); | |
++line_position; | |
} | |
} | |
} | |
void output_spaces(int how_many) | |
{ | |
int i; | |
for (i = 0; i < how_many; i++) { | |
putchar(' '); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment