Jump to: navigation, search

The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 1.10 on page 20

Write a program to copy its input to its output, replacing each tab by \t , each backspace by \b , and each backslash by \\ . This makes tabs and backspaces visible in an unambiguous way.



Solution by Vidhan Gupta

/* Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b,
and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way. */

int main(){

    int c;
    while ((c = getchar()) != EOF)
    {
        if (c == '\t')
        {
            putchar('\\');
            c = 't';
        }
        if (c == '\b')
        {
            putchar('\\');
            c = 'b';
        }
        if (c == '\\')
        {
            putchar('\\');
            c = '\\';
        }
        
        putchar(c);
        
    }
    
    return 0;
}
OUTPUT:
Hell
Hell
Hell \          hell
Hell \\ \t\thell


Solution by Gregory Pietsch (Category 0)

Gregory Pietsch pointed out that Richard Heathfield's solution was actually Category 1. He was quite right. Better still, he was kind enough to submit a Category 0 solution himself. Here it is:

/* Gregory Pietsch */

/*
 * Here's my attempt at a [[K&R2 solutions:Ancillary:Category numbers|Category 0]] version of 1-10.
 *
 * Gregory Pietsch
 */

#include <stdio.h>

int main()
{
    int c, d;

    while ( (c=getchar()) != EOF) {
        d = 0;
        if (c == '\\') {
            putchar('\\');
            putchar('\\');
            d = 1;
        }
        if (c == '\t') {
            putchar('\\');
            putchar('t');
            d = 1;
        }
        if (c == '\b') {
            putchar('\\');
            putchar('b');
            d = 1;
        }
        if (d == 0)
            putchar(c);        
    }
    return 0;
}

Solution by Richard Heathfield (Category 1)

This solution, which Richard Heathfield wrote, is the sadly discredited Cat 0 answer which has found a new lease of life in Category 1.

#include <stdio.h>

#define ESC_CHAR '\\'

int main(void)
{
  int c;

  while((c = getchar()) != EOF)
  {
    switch(c)
    {
      case '\b':
        /* The OS on which I tested this (NT) intercepts \b characters. */
        putchar(ESC_CHAR);
        putchar('b');
        break;
      case '\t':
        putchar(ESC_CHAR);
        putchar('t');
        break;
      case ESC_CHAR:
        putchar(ESC_CHAR);
        putchar(ESC_CHAR);
        break;
      default:
        putchar(c);
        break;
    }
  }
  return 0;
}

Solution by El maachi ayman

Here's my solution. I believe it's the best one since the program uses less instructions and variables


/*
 * Here's my attempt at a [[K&R2 solutions:Ancillary:Category numbers|Category 2]] version of 1-10.
 *
 */

#include <stdio.h>

int main()
{
       int c;

    while((c=getchar())!=EOF)
{
	if(c== '\\' || c=='\t' || c=='\b')
		{
			putchar('\\');
			if(c == '\\')
				putchar('\\');
			if(c=='\t')
				putchar('t');
			if(c=='\b')
				putchar('b');

		}


	else
		putchar(c);
}
    return 0;
}


Personal tools