C code, but the logic is the same with C++

This is C code, but the logic is the same with C++.
I can output something, but there is one case I don't know how to do.

when the input is \n \n \r \r \n ( no changes for this), the output should not change if there is even \n or \r. For example, \n \n \r \r \r \n, \r's should change to \n because there's odd \r, but when there is even, it should not change. How can I write the code for that? Hint please!

1
2
3
4
5
6
7
8
9
10

       if (shift == 1)
        { c0 = c1; c1 = getc(stdin);}
        else                                          /* shift == 2 */
        { 
	  c0 = getc(stdin); 
	  c1 = getc(stdin);

        }


can someone explain this part for me? I am not very clear, I was told it is the same concept as shift register. what are c0 and c1 pointing initially? how does it work for shift 2? Thanks!

-------------------------------------------------------------------------------

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

    while (c0 != EOF) 
    {
        int shift = 1;
        

        putc(c0, stdout);

        if (shift == 1)
        { c0 = c1; c1 = getc(stdin);}
        else                                          /* shift == 2 */
        { 
	  c0 = getc(stdin); 
	  c1 = getc(stdin);

        }
	
        if(c0 == '\f' || c0 == '\v' || c0 == '\t')
	{ c0 = ' '; }
	
	
	if(c0 == '\r')
	{
	    if(c1 == '\r')
	    {
	        c0 = 'r';
	    } 
	    else if(c1 == '\n')
	    {
		c0 = '\n';
		c1 = ' ';
	    }
	}
    }





a  \f   1  \v   2  \t   3  \n   b  \r   c  \r  \n  \n  \n  \r  \r  \n

a       1       2       3  \n   b  \r   c  \n      \n  \n   r  \n
Last edited on
Topic archived. No new replies allowed.