Aug 28, 2016

K & R : Exercise 1.20 - detab

= K & R : Exercise 1.20 - detab

Problem Statement

Exercise 1-20: Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop.


Solution

We need to take each character and check if it a tab and replace multiple spaces. Solution to above problem would look something like this.

#include <stdio.h>
#define SPACE_FOR_TAB 4
int main()
{
  int ch, i;

  while((ch = getchar()) != EOF) {
    if(ch == '\t') {
      for(i = 0; i < SPACE_FOR_TAB; i++) {
        putchar(' ');
      }
    }else{
      putchar(ch);
    }
  }
  return 0;
}

No comments :

Post a Comment