Please tell me if I've written this correctly

S

Sathyaish

Hi friends,

I am beginning to practice C from K&R. Although I am only familiar
with the language, this time I want to invest time learning it
thoroughly. I've written this practice excercise program to replace
more than a single blank in an input with only a single blank in the
output. Here's the code.

#include <stdio.h>

//A program that displays keyboard input to the monitor, but trimming
more than a single space to a single space
//I've not replaced tabs with single spaces yet

#define SPACE 1

void main()
{

int c, state;
c=0;
state=SPACE;
while ((c=getchar()) != EOF)
{
if (!(state && (c==' '))) putchar(c);

if (c==' ')
{
state=SPACE;
}
else
{
state= !SPACE;
}

}
}

Although I've tested it four to five times, I don't know if I can be
sure as to whether it is right, since I've made guesses about the
BITWISE operators such as NOT to be the same as the logical NOT ! and
the BITWISE AND to be the same as the LOGICAL AND (&&). Also, on
purpose, I have not yet detected tabbed spaces.

Could you gurus please tell me if I've written this program correctly?
 
N

Nils Petter Vaskinn

Although I've tested it four to five times, I don't know if I can be
sure as to whether it is right, since I've made guesses about the
BITWISE operators such as NOT to be the same as the logical NOT ! and
the BITWISE AND to be the same as the LOGICAL AND (&&). Also, on
purpose, I have not yet detected tabbed spaces.

Logical NOT !
Bitwise NOT ~
Logical AND &&
Bitwise AND &

They are in section 2.9 of K&R second edition. Although there is no need
for bitwise operators for your program.
Could you gurus please tell me if I've written this program correctly?

Aside from the "void main()" issue it appears to be correct. Here's how I
would write it:


#include <stdio.h>
int main(){
/* main can't be void */
int c, space;
space = 0;
while ((c=getchar()) != EOF) {
/* compare c to space first, since the input will probably be
mostly non space this will save us one comparison for
each character */
if ((c != ' ') || !space) putchar(c);
/* set space true if c was a space otherwise false */
space = (c == ' ');
}
}
 
O

osmium

Sathyaish said:
Although I've tested it four to five times, I don't know if I can be
sure as to whether it is right, since I've made guesses about the
BITWISE operators such as NOT to be the same as the logical NOT ! and
the BITWISE AND to be the same as the LOGICAL AND (&&). Also, on
purpose, I have not yet detected tabbed spaces.

Bitwise and logical things are fairly disjoint. WRT logical things, a value
of 0 is treated as logical false, anything else is true. This is not a
definition that one immediately falls in love with! The bitwise operators
do not recognize true or false, they are just numbers. Remember that there
are not "good" numbers and "bad" numbers. I ssume you realize the bitwise
NOT you refer to is denoted by ~.
 
P

pete

Nils said:
Logical NOT !
Bitwise NOT ~
Logical AND &&
Bitwise AND &

They are in section 2.9 of K&R second edition. Although there is no need
for bitwise operators for your program.


Aside from the "void main()" issue it appears to be correct. Here's how I
would write it:

#include <stdio.h>
int main(){
/* main can't be void */
int c, space;
space = 0;
while ((c=getchar()) != EOF) {
/* compare c to space first, since the input will probably be
mostly non space this will save us one comparison for
each character */
if ((c != ' ') || !space) putchar(c);
/* set space true if c was a space otherwise false */
space = (c == ' ');
}
}

/* BEGIN practice.c */

#include <stdio.h>

#define SPACE 1

int main(void)
{
int c, old_state, new_state;

old_state = SPACE;
c = getchar();
while (c != EOF) {
new_state = c == ' ';
if (!(new_state && old_state)) {
putchar(c);
}
old_state = c == '\n' ? SPACE : new_state;
c = getchar();
}
return 0;
}

/* END practice.c */
 
N

nrk

pete said:
/* BEGIN practice.c */

#include <stdio.h>

#define SPACE 1

int main(void)
{
int c, old_state, new_state;

old_state = SPACE;
c = getchar();
while (c != EOF) {
new_state = c == ' ';
if (!(new_state && old_state)) {
putchar(c);
}
old_state = c == '\n' ? SPACE : new_state;
c = getchar();
}
return 0;
}

/* END practice.c */

#include <stdio.h>

int main(void) {
int prev = 0;
int c;

while ( (c = getchar()) != EOF ) {
if ( c != ' ' || prev != ' ' )
putchar(c);
prev = c;
}

return 0;
}

-nrk.
 
S

Sathyaish

/********************************************************************
if (c==' ')
{
state=SPACE;
}
else
{
state= !SPACE;
}
*/
//I could better write the above if construct as
state = (c==' ');
//**********************************************************************


Regards,
Sathyaish Chakravarthy
 
M

Mark A. Odell

(e-mail address removed) (Sathyaish) wrote in

/********************************************************************
if (c==' ')
{
state=SPACE;
}
else
{
state= !SPACE;
}
*/
//I could better write the above if construct as
state = (c==' ');

What if SPACE was set to 17? Below would better match the if-else block:

state = ' ' == c ? SPACE : !SPACE;
 
P

pete

Mark said:
(e-mail address removed) (Sathyaish) wrote in


What if SPACE was set to 17?

I think he's removing SPACE from the code,
as nrk did elsewhere in this thread.
 
P

Peter Shaggy Haywood

Groovy hepcat Sathyaish was jivin' on 2 Mar 2004 02:56:44 -0800 in
comp.lang.c.
Please tell me if I've written this correctly's a cool scene! Dig it!
void main()

You have been told before that main() is supposed to return an int.
Pay attention to what you are told, lest you be ignored!

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top