By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

HELP!! Two questions from <<the c programming language>>

P: n/a
Tak
Exercise 1-9. Write a program to copy its input to its output,
replacing each string of one or
more blanks by a single blank.

Exercise 1-10. 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.

Who could sent me the codes, Thanks very much! My email:
ka******@gmail.com

May 31 '07 #1
Share this Question
Share on Google+
27 Replies


P: n/a
Tak wrote:
Exercise 1-9. Write a program to copy its input to its output,
replacing each string of one or
more blanks by a single blank.

Exercise 1-10. 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.

Who could sent me the codes, Thanks very much! My email:
ka******@gmail.com
No, that's not how it works here. You post your attempt, we help you
with problems. We don't do homework.

--
Ian Collins.
May 31 '07 #2

P: n/a
Tak wrote:
Exercise 1-9. Write a program to copy its input to its output,
replacing each string of one or
more blanks by a single blank.

Exercise 1-10. 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.

Who could sent me the codes, Thanks very much! My email:
ka******@gmail.com
The /point/ about doing the exercises is to /learn by doing/. If
you don't do the work, the learning doesn't happen, or doesn't
happen as well.

Do the work. We can crit the result, but you have to show willing.

--
JUC 2007, submit: http://hpl.hp.com/conferences/juc2007/submission.html

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

May 31 '07 #3

P: n/a
Tak
No, that's not how it works here. You post your attempt, we help you
with problems. We don't do homework.

--
Ian Collins.


No, I think you are misunderstanding.
It's not homework;

I can't work out Exercise 1-9.
My code of Exercise 1-10 is like this:

#include <stdio.h>

main()
{
char c;

while ((c = getchar()) != EOF)
{
if (c == '\t')
printf("\\t");
else if (c == '\b')
printf("\\b");
else if (c == '\\')
printf("\\\\");
else
putchar(c);
}
}
I don't know whether it's right or not?

May 31 '07 #4

P: n/a
Tak wrote:
>No, that's not how it works here. You post your attempt, we help you
with problems. We don't do homework.

--
Ian Collins.

No, I think you are misunderstanding.
It's not homework;

I can't work out Exercise 1-9.
You have the basic structure, so you should be able to change your loop
to spot and filter spaces.
My code of Exercise 1-10 is like this:

#include <stdio.h>

main()
main returns int, if you compiler didn't warn you, turn up the warnings
until it does.
{
char c;

while ((c = getchar()) != EOF)
{
if (c == '\t')
printf("\\t");
else if (c == '\b')
printf("\\b");
else if (c == '\\')
printf("\\\\");
else
putchar(c);
}
}
I don't know whether it's right or not?
It looks OK, but have you tried it? It should be easy enough to test.

--
Ian Collins.
May 31 '07 #5

P: n/a
Tak
I know how to do, Thank you!

May 31 '07 #6

P: n/a
Tak wrote:
I know how to do, Thank you!
Good. Next time, please keep some context from the message you are
replying to.

--
Ian Collins.
May 31 '07 #7

P: n/a
Ian Collins <ia******@hotmail.comwrites:
Tak wrote:
[...]
>{
char c;

while ((c = getchar()) != EOF)
[snip]
>I don't know whether it's right or not?
It looks OK, but have you tried it? It should be easy enough to test.
What type does getchar() return?

See also section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 31 '07 #8

P: n/a
On May 31, 1:55 pm, Tak <kakat...@gmail.comwrote:
No, that's not how it works here. You post your attempt, we help you
with problems. We don't do homework.
--
Ian Collins.

No, I think you are misunderstanding.
It's not homework;

I can't work out Exercise 1-9.
My code of Exercise 1-10 is like this:

#include <stdio.h>

main()
{
char c;

while ((c = getchar()) != EOF)
{
if (c == '\t')
printf("\\t");
else if (c == '\b')
printf("\\b");
else if (c == '\\')
printf("\\\\");
else
putchar(c);
}

}

I don't know whether it's right or not?
everything is ok...why don't you run this and see?

why you can't work out 1-9 ?? I can give you idea, you implement it.

accept the char.. if it is a space set a flag saying you got one
space. you should ignore further spaces if this flag is set, otherwise
display further chars.
Continue this till eof.

Huuuh...didn't get idea???? Follow the step

Algorithm trim_space

while EOF
do
begin
input char
if char = space then
begin
if flag = 0 then
begin
let flag = 1;
print char;
end
end
else
begin
let flag = 0
print char
end
end
goto step 2
end while

Bye
Guru Jois

May 31 '07 #9

P: n/a
Tak wrote:
>No, that's not how it works here. You post your attempt, we help you
with problems. We don't do homework.
(fx:hint (fx:snipped-signature))
No, I think you are misunderstanding.
It's not homework;

I can't work out Exercise 1-9.
My code of Exercise 1-10 is like this:

#include <stdio.h>

main()
{
char c;
/int/ c.
while ((c = getchar()) != EOF)
`getchar` returns an `int`, because it must be able to return
every character value /and one more/, viz EOF. (Yes, this
presents problems on systems where `sizeof(int) == sizeof(char)`.)
I don't know whether it's right or not?
Does it pass your test cases?

--
"You've spotted a flaw in my thinking, Trev" Big Al,/The Beiderbeck Connection/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 31 '07 #10

P: n/a
Guru Jois wrote:

A bunch of not-C, for no terribly good reason but:
while EOF
I don't think so.
do
begin
input char
if char = space then
begin
if flag = 0 then
No declaration for `flag`; hence, no know state.
begin
let flag = 1;
print char;
end
end
else
begin
let flag = 0
print char
end
end
goto step 2
You have no step 2 (in fact you have no steps).

You don't need `goto` for this problem, especially if you're
going to write in an invented pseudocode.
end while
Make up your mind whether you're using begin-end blocks or
whether your control-structures have end-Whatever syntax.

--
"He's dead, Jim, but not as we know it." Unsaid /Trek/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

May 31 '07 #11

P: n/a
On May 31, 2:24 pm, Chris Dollin <chris.dol...@hp.comwrote:
Guru Jois wrote:

A bunch of not-C, for no terribly good reason but:
while EOF

I don't think so.
do
begin
input char
if char = space then
begin
if flag = 0 then

No declaration for `flag`; hence, no know state.
begin
let flag = 1;
print char;
end
end
else
begin
let flag = 0
print char
end
end
goto step 2

You have no step 2 (in fact you have no steps).

You don't need `goto` for this problem, especially if you're
going to write in an invented pseudocode.
end while

Make up your mind whether you're using begin-end blocks or
whether your control-structures have end-Whatever syntax.

--
"He's dead, Jim, but not as we know it." Unsaid /Trek/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
just a steps to that asker... sir. I wrote it as steps, but later
removed, but forget this one....
Please identify notable mistakes...not a silly ones

Bye
Guru Jois

May 31 '07 #12

P: n/a
Tak
Algorithm trim_space
>
while EOF
do
begin
input char
if char = space then
begin
if flag = 0 then
begin
let flag = 1;
print char;
end
end
else
begin
let flag = 0
print char
end
end
goto step 2
end while
My program of exercise 1-9

#include <stdio.h>

int main()
{
int flag=1;
int c;

while ((c = getchar()) != EOF)
{
if (c == ' ')
{
flag = 0;
}
else if (flag == 0)
{
printf(" ");
putchar(c);
flag = 1;
}
else
putchar(c);
}
}

It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?

May 31 '07 #13

P: n/a
Guru Jois wrote:
On May 31, 2:24 pm, Chris Dollin <chris.dol...@hp.comwrote:
>Guru Jois wrote:

A bunch of not-C, for no terribly good reason but:
SNIP SIGNATURES.
just a steps to that asker... sir. I wrote it as steps, but later
removed, but forget this one....
Please identify notable mistakes...not a silly ones
(a) I did. The very first line is a notable mistake, rendering the
algorithm useless:
while EOF
(b) Computers can't tell the difference between a silly mistake and
a notable one.

(c) Programmers have to take care /all the time/. Sloppiness feeds
on itself.

--
"It is seldom good news." ~Crystal Ball~, /The Tough Guide to Fantasyland/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

May 31 '07 #14

P: n/a
Tak <ka******@gmail.comwrites:
>Algorithm trim_space
<bad algorithm snipped>
>
My program of exercise 1-9

#include <stdio.h>

int main()
{
int flag=1;
int c;

while ((c = getchar()) != EOF)
{
if (c == ' ')
{
flag = 0;
}
else if (flag == 0)
{
printf(" ");
putchar(c);
flag = 1;
}
else
putchar(c);
}
}

It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?
Yes. What happens when the input ends with one or more spaces?

--
Ben.
May 31 '07 #15

P: n/a
Tak wrote:
>
I know how to do, Thank you!
How to do what? Read the following sig.

--
If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.
More details at: <http://cfaj.freeshell.org/google/>

--
Posted via a free Usenet account from http://www.teranews.com

May 31 '07 #16

P: n/a
On May 31, 6:04 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Tak <kakat...@gmail.comwrites:
Algorithm trim_space

<bad algorithm snipped>


My program of exercise 1-9
#include <stdio.h>
int main()
{
int flag=1;
int c;
while ((c = getchar()) != EOF)
{
if (c == ' ')
{
flag = 0;
}
else if (flag == 0)
{
printf(" ");
putchar(c);
flag = 1;
}
else
putchar(c);
}
}
It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?

Yes. What happens when the input ends with one or more spaces?

--
Ben.
yet smaller code..

#include<stdio.h>
main() {

int c;
int flag;

flag = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ') {
if (!flag) {
flag = 1;
putchar (c);
}
}
else {
flag = 0;
putchar (c);
}
}

}

Bye
Guru Jois

May 31 '07 #17

P: n/a
On May 31, 3:27 pm, Tak <kakat...@gmail.comwrote:
Algorithm trim_space
while EOF
do
begin
input char
if char = space then
begin
if flag = 0 then
begin
let flag = 1;
print char;
end
end
else
begin
let flag = 0
print char
end
end
goto step 2
end while

My program of exercise 1-9

#include <stdio.h>

int main()
{
int flag=1;
int c;

while ((c = getchar()) != EOF)
{
if (c == ' ')
{
flag = 0;
}
else if (flag == 0)
{
printf(" ");
putchar(c);
flag = 1;
}
else
putchar(c);
}

}

It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?
yet smaller code..

#include<stdio.h>
main() {

int c;
int flag;

flag = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ') {
if (!flag) {
flag = 1;
putchar (c);
}
}
else {
flag = 0;
putchar (c);
}
}

}

Bye
Guru Jois

May 31 '07 #18

P: n/a
Guru Jois <gu*******@gmail.comwrites:
On May 31, 6:04 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>Tak <kakat...@gmail.comwrites:
>Algorithm trim_space

<bad algorithm snipped>
>>
My program of exercise 1-9
#include <stdio.h>
int main()
{
<snipped>
}
It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?

Yes. What happens when the input ends with one or more spaces?

yet smaller code..

#include<stdio.h>
main() {

int c;
int flag;

flag = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ') {
if (!flag) {
flag = 1;
putchar (c);
}
}
else {
flag = 0;
putchar (c);
}
}

}
I'd write it:

#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
}
return 0;
}

because I am not a fan of flag variables and this pattern is very
useful for all sorts of filtering and counting programs.

--
Ben.
May 31 '07 #19

P: n/a
On May 31, 6:04 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Tak <kakat...@gmail.comwrites:
Algorithm trim_space

<bad algorithm snipped>


My program of exercise 1-9
#include <stdio.h>
int main()
{
int flag=1;
int c;
while ((c = getchar()) != EOF)
{
if (c == ' ')
{
flag = 0;
}
else if (flag == 0)
{
printf(" ");
putchar(c);
flag = 1;
}
else
putchar(c);
}
}
It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?

Yes. What happens when the input ends with one or more spaces?

--
Ben.
yet smaller code..

#include<stdio.h>
main() {

int c;
int flag;

flag = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ') {
if (!flag) {
flag = 1;
putchar (c);
}
}
else {
flag = 0;
putchar (c);
}
}

}

Bye
Guru Jois

Jun 1 '07 #20

P: n/a
On May 31, 3:27 pm, Tak <kakat...@gmail.comwrote:
Algorithm trim_space
while EOF
do
begin
input char
if char = space then
begin
if flag = 0 then
begin
let flag = 1;
print char;
end
end
else
begin
let flag = 0
print char
end
end
goto step 2
end while

My program of exercise 1-9

#include <stdio.h>

int main()
{
int flag=1;
int c;

while ((c = getchar()) != EOF)
{
if (c == ' ')
{
flag = 0;
}
else if (flag == 0)
{
printf(" ");
putchar(c);
flag = 1;
}
else
putchar(c);
}

}

It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?
yet smaller code..

#include<stdio.h>
main() {

int c;
int flag;

flag = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ') {
if (!flag) {
flag = 1;
putchar (c);
}
}
else {
flag = 0;
putchar (c);
}
}

}

Bye
Guru Jois

Jun 1 '07 #21

P: n/a
Tak
On 5月31日, 下午10时08分, CBFalconer <cbfalco...@yahoo.comwrote:
Tak wrote:
I know how to do, Thank you!

How to do what? Read the following sig.

--
If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.
More details at: <http://cfaj.freeshell.org/google/>

--
Posted via a free Usenet account fromhttp://www.teranews.com
Sorry, I am new here.

Jun 1 '07 #22

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukha scritto nel messaggio
news:87************@bsb.me.uk...
#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?
}
return 0;
}

Jun 2 '07 #23

P: n/a
Army1987 wrote:
"Ben Bacarisse" <be********@bsb.me.ukha scritto nel messaggio
news:87************@bsb.me.uk...
>#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
> while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?
Assuming the implementation doesn't actively hinder the simplest I/O, it
gets converted to a negative value which will not compare equal to ' '.
> }
return 0;
}
Jun 2 '07 #24

P: n/a
Harald van D某k <tr*****@gmail.comwrites:
Army1987 wrote:
>"Ben Bacarisse" <be********@bsb.me.ukha scritto nel messaggio
news:87************@bsb.me.uk...
>>#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
>> while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?

Assuming the implementation doesn't actively hinder the simplest I/O, it
gets converted to a negative value which will not compare equal to '
'.
Indeed. But the suggestion to use int last_char is, I think, still a
good one. It would allow the more obvious initial setting of EOF and
would simplify matters if one were to require character class testing
(if (!isspace(last_char)... and so on).

I had originally used int and changed it for no good reason as far as
I can see.

--
Ben.
Jun 2 '07 #25

P: n/a
On Sat, 2 Jun 2007 15:19:41 +0200, "Army1987" <pl********@for.it>
wrote:
>
"Ben Bacarisse" <be********@bsb.me.ukha scritto nel messaggio
news:87************@bsb.me.uk...
>#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
> while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?
Is getchar not guaranteed to return the integer value of either a
"proper" char or EOF? If 163 will not fit in a char and since EOF is
guaranteed to be negative, is it even possible for getchar to return
this value?
> }
return 0;
}

Remove del for email
Jun 2 '07 #26

P: n/a
Barry Schwarz wrote:
On Sat, 2 Jun 2007 15:19:41 +0200, "Army1987" <pl********@for.it>
wrote:
>>
"Ben Bacarisse" <be********@bsb.me.ukha scritto nel messaggio
news:87************@bsb.me.uk...
>>#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
>> while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?

Is getchar not guaranteed to return the integer value of either a
"proper" char or EOF? If 163 will not fit in a char and since EOF is
guaranteed to be negative, is it even possible for getchar to return
this value?
getchar does not work with plain chars, it works with unsigned chars. 163
fits just fine in an unsigned char, so getchar is allowed to return 163.
>> }
return 0;
}
Jun 2 '07 #27

P: n/a

"Barry Schwarz" <sc******@doezl.netha scritto nel messaggio
news:17********************************@4ax.com...
On Sat, 2 Jun 2007 15:19:41 +0200, "Army1987" <pl********@for.it>
wrote:
>>
"Ben Bacarisse" <be********@bsb.me.ukha scritto nel messaggio
news:87************@bsb.me.uk...
>>#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
>> while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?

Is getchar not guaranteed to return the integer value of either a
"proper" char or EOF? If 163 will not fit in a char and since EOF is
guaranteed to be negative, is it even possible for getchar to return
this value?
Assuming sizeof(int) 1, it isn't.
getchar() will return either EOF or a valid *unsigned* char, which
can't of course equal EOF.
Jun 3 '07 #28

This discussion thread is closed

Replies have been disabled for this discussion.