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

T

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:
(e-mail address removed)
 
I

Ian Collins

Tak said:
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:
(e-mail address removed)
No, that's not how it works here. You post your attempt, we help you
with problems. We don't do homework.
 
C

Chris Dollin

Tak said:
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:
(e-mail address removed)

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.
 
T

Tak

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



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?
 
I

Ian Collins

Tak said:
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.
 
C

Chris Dollin

(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?
 
G

Guru Jois

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
 
C

Chris Dollin

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.
 
G

Guru Jois

Guru Jois wrote:

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


I don't think so.


No declaration for `flag`; hence, no know state.


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.


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
 
T

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?
 
C

Chris Dollin

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:

(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.
 
B

Ben Bacarisse

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?
 
C

CBFalconer

Tak said:
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/>
 
G

Guru Jois

<bad algorithm snipped>










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);
}
}

}

Bye
Guru Jois
 
G

Guru Jois

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
 
B

Ben Bacarisse

Guru Jois said:
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.
 
G

Guru Jois

<bad algorithm snipped>










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);
}
}

}

Bye
Guru Jois
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top