#include <stdio.h>
/* 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. */
main()
{
int c, flags;
flags = false;
while ((c = getchar()) != EOF)
{
switch (c) {
case'\t':
printf("\t");
flags = true;
case'\\':
printf("\\");
flags = true;
case'\b':
printf("\b");
flags = true;
}
if (flags = 0) { putchar(c); }
}
return 0;
}
This the code I generated myself, but once again running into unexpected things during compile time. Anyone spot some errors that I have possibly made?
You need to set flags to false each time through the loop. Without resetting flags, it stays true for the rest of the program. Actually, you do set flags to 0 in line 37, but that's not the correct way to do it and that has other consequences.
Line 37: You're using the assignment operator (-), not the equality (==) operator.
This will cause flags to be set to 0, and the if will always evaluate to false, therefore, the putchar is never evecuted.
/* 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. */
#include <stdio.h>
#define ESC_CHAR '\'
main()
{
int c;
while ((c = getchar()) != EOF)
{
switch(c)
{
case'\b':
putchar(ESC_CHAR);
putchar('b');
case'\t':
putchar(ESC_CHAR);
putchar('t');
case ESC_CHAR:
putchar(ESC_CHAR);
putchar(ESC_CHAR);
default:
putchar(c);
break;
}
}
return 0;
}
This was my current try, but didn`t compile at all.
/* 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. */
#include <stdio.h>
int main()
{
int c;
while ((c = getchar()) != EOF)
{
switch(c)
{
case'\b':
printf("\\b");
break;
case'\t':
printf("\\t");
break;
case'\\':
printf("\\\\");
break;
default:
putchar(c);
break;
}
}
return 0;
}