PDA

View Full Version : beginner question in C ?




mr.tolgao
Jun 17, 2011, 02:51 AM
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:

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:



jiminaus
Jun 17, 2011, 03:10 AM
why do you need putchar( ' \\ ') ? What does it do ?

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.


also what is the purpose of variable d ?

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.


Also, Is there another way to write this code ?


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.

mr.tolgao
Jun 17, 2011, 08:22 AM
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:

notjustjay
Jun 17, 2011, 09:34 AM
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.

mr.tolgao
Jun 17, 2011, 10:22 AM
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.

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

notjustjay
Jun 17, 2011, 11:03 AM
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.
:)

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 :)

Oligarch
Jun 17, 2011, 03:13 PM
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.