Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Exercise 1-21.
- * Write a program entab that replaces strings of blanks by the minimum number
- * of tabs and blanks to achieve the same spacing. Use the same tab stops as for
- * detab. When either a tab or a single blank would suffice to reach a tab stop,
- * which should be given preference?
- */
- #include <stdio.h>
- #define TABSTOP 8
- int
- main() {
- int c;
- int offset = 0;
- int spaces = 0;
- while ((c = getchar()) != EOF) {
- if (c == ' ') {
- spaces++;
- } else {
- if (spaces) {
- if (spaces + (offset%TABSTOP) > TABSTOP) {
- spaces += (offset%TABSTOP);
- }
- offset += spaces;
- while (spaces > TABSTOP) {
- putchar('\t');
- spaces -= TABSTOP;
- }
- while (spaces) {
- spaces--;
- putchar(' ');
- }
- }
- if (c == '\n')
- offset = 0;
- else if (c == '\t')
- offset += TABSTOP - (offset%TABSTOP);
- else
- offset++;
- putchar(c);
- }
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment