help with exercise



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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.

Damn, but I`m not even sure how to generate backspace??

How would I generate tab and backslash?

Backslash is probably alt + 7 if I`m not too wrong.
Last edited on

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

/* 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.
Last edited on
Line 10: The backslash must be escaped.

 
#define ESC_CHAR '\\' 


Line 13: main() must always be type int.

Now when thinking about it, in my opinion this is the most stylish version I came up so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

/* 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;
   
 }




Further documented in

https://www.gnu.org/software/guile/manual/html_node/Backslash-Escapes.html
Topic archived. No new replies allowed.