solution to Knr Exercise 1-9

S

sathyashrayan

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"

I got the following solution from the net

<url>

http://users.powernet.co.uk/eton/kandr2/krx109.html

</url>


#include <stdio.h>

int main(void)
{
int c;
while ((c = getchar()) != EOF) {
if (c == ' ') {
putchar(c);
while((c = getchar()) == ' ' && c != EOF)
;
}
if (c == EOF)
break; /* the break keyword is mentioned
* in the introduction...
* */

putchar(c);
}
return 0;
}

When I run this code the program does not terminate. It keeps
getting the input from the stdin. When I type more than one blank space
the program simply prints the set of char as it is including more than
one blank. Is the program wrong or I am missing something?

--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com
 
J

John Valko

sathyashrayan said:
When I run this code the program does not terminate. It keeps
getting the input from the stdin.

The problem is not with your code but rather with how you are feeding
the program input. If you're on a *nix based OS try hitting ctrl+d to
send an EOF. On windows, try ctrl+z.

--John
 
C

CBFalconer

John said:
The problem is not with your code but rather with how you are
feeding the program input. If you're on a *nix based OS try
hitting ctrl+d to send an EOF. On windows, try ctrl+z.

Those signals may only be accepted immediately after the
end-of-line, i.e. a <cr> or <enter>. He can also try redirecting
input to come from a real disk file, which has an end somewhere.
 
T

Taran

sathyashrayan 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"

I got the following solution from the net

<url>

http://users.powernet.co.uk/eton/kandr2/krx109.html

</url>


#include <stdio.h>

int main(void)
{
int c;
while ((c = getchar()) != EOF) {

the code is great and works fine. The termination is decided on what
input you give at command which is compared to EOF. Find EOF for you
system. Enter it when you want to end the program.
if (c == ' ') {
putchar(c);
while((c = getchar()) == ' ' && c != EOF)
;
}
if (c == EOF)
break; /* the break keyword is mentioned
* in the introduction...
* */

putchar(c);
}
return 0;
}

When I run this code the program does not terminate. It keeps
getting the input from the stdin. When I type more than one blank space
the program simply prints the set of char as it is including more than
one blank. Is the program wrong or I am missing something?

--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

HTH

Regards,
Taran
TT
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top