beginner question in C ?

Discussion in 'Mac Programming' started by mr.tolgao, Jun 17, 2011.

  1. mr.tolgao, Jun 17, 2011
    Last edited by a moderator: Jun 17, 2011

    macrumors newbie

    Joined:
    Oct 18, 2010
    #1
    Hello,

    I am learning C through the book
    "The C Programming Language", 2nd edition, Kernighan and Ritchie


    I am on exercise, 1-10. I did not understand the below answer in this exercise.

    Question:
    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.

    Answer:

    Code:
    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;
    }
    Why do you use two putchar statements:

    for example :

    putchar('\\');
    putchar('t');
    why do you need putchar( ' \\ ') ? What does it do ?

    also what is the purpose of variable d ?

    Also, Is there another way to write this code ?


    Thanks,
    Tolga :confused:
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    It puts out a single backslash. Think about what the compiler would try to interpret if you put only a single backslash? Hint: Think about how backslashes are used in C, eg \t and \n.

    Read through the code statement by statement. Think of different inputs and trace through the loop looking for how d changes and how it effects the flow of the program. The purpose of d should become apparent.

    There's always another way to write the code. Did you try this yourself before looking at the answer? If you didn't, you've lost a very valuable learning experience. At the very least, learn to you read other people's code, so when you copy it you can at least use it and adapt it correctly.
     
  3. thread starter macrumors newbie

    Joined:
    Oct 18, 2010
    #3
    Thank you for making me think without giving me a direct answer.

    By the way, I tried my solution before. But it did not work so I looked at the answer. :rolleyes:
     
  4. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #4
    It may help to write "pseudo code" when trying to come up with algorithms to solve problems.

    For example, the problem statement was this:

    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.

    So without even writing a single line of real code, and without concern for whether that code is going to be written in C, Python, Perl, BASIC, or whatever, you can still start solving it.

    You might write some pseudocode that looks like this:

    open an input stream
    loop for every character in the stream
    read the character
    is it a tab character? if so then output "\t"
    else is it a backspace character? if so then output "\b"
    else is it a backslash character? if so then output "\\"
    else (if none of the above) output whatever the original character was
    end loop

    Any programming problem actually involves TWO problems:
    - what, exactly, am I trying to do?
    - how do I write the code to get the computer to do it?

    Once you've got an idea of what steps are necessary to solve this problem, then you can start writing C code.

    Being able to come up with pseudocode is a key skill, and I highly recommend you spend some time working on a "plain english" solution to the problem before you start translating it into C code.
     
  5. thread starter macrumors newbie

    Joined:
    Oct 18, 2010
    #5
    Thank you for giving me tips about how to write programs. You really hit the nail regarding the process of writing codes. I really have hard time to write pseudocode. I quickly lose focus of the big picture and instead get lost in details of the program. As you said, it is quite a skill to develop. I am kind of impatient too. Just want to get it done quickly without writing pseudocodes. Thanks again for your input.
    :)
     
  6. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #6
    Glad it was helpful. Eventually you won't need to physically write down pseudocode for simpler things because as you become more skilled as a programmer, you'll be able to visualize the pseudocode in your head. But you're still doing it in your head. The trick is to train your mind to "think like a computer", and until you're good at that, then there is a risk that when you sit down at a keyboard and start writing code, you're trying to tackle two different problems at once and not getting very far with either one.

    Good luck :)
     
  7. macrumors newbie

    Joined:
    Nov 10, 2008
    #7
    Here's another hint, Tolga:

    Begin at the end.

    And work your way backwards. Losing focus impossible. Usually much quicker than going the other way.
     

Share This Page