K&R2, 1.5.3, exercise 1-9

A

arnuld

it works fine. any advice for improvement:

---------- PROGRAMME --------------
/* K&R2: section 1.5.3, exercise 1-9

STATEMENT:
write a programme to copy its input to its output
replacing a string of one more blanks with a single blank.

*/

#include<stdio.h>

#define IN 1
#define OUT 0

int main()
{
int c = 0;
int state = OUT;

while((c = getchar()) != EOF)
{
if(c == ' ')
{
if(state == OUT)
{
state = IN;
putchar(c);
}
}

if(c != ' ')
{
putchar(c);
state = OUT;
}
}

return 0;
}

------------- OUTPUT ------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra ex_1-9.c
[arch@voodo kr2]$ ./a.out
like this
like this
like this
like this
like this
like this
like this
like this
like this
like this
[arch@voodo kr2]$
 
R

Richard Heathfield

arnuld said:
it works fine. any advice for improvement:

Two points. Firstly, as a matter of good style, it is best to provide
full function prototypes for your functions. I know that, at present,
you only have one function - main - but you may as well start as you
mean to go on, and write it as:

int main(void)

Secondly, your rather odd (GNU, is it?) indent style is up to you, but
you need to be aware that newsreaders don't like tabs. Here is the
indent structure that my newsreader showed me for your code:
{
{
{
{
}
}
{
}
}
}

which isn't what you intended, I think. If you use spaces instead of
tabs, newsreaders tend to respect that.
 

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,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top