Re: Please tell me if I've written this correctly
From: pete (pfiland_at_mindspring.com)
Date: 03/02/04
- Next message: nrk: "Re: Please tell me if I've written this correctly"
- Previous message: Jeff: "Re: Is there a difference?"
- In reply to: Nils Petter Vaskinn: "Re: Please tell me if I've written this correctly"
- Next in thread: nrk: "Re: Please tell me if I've written this correctly"
- Reply: nrk: "Re: Please tell me if I've written this correctly"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 02 Mar 2004 13:29:20 GMT
Nils Petter Vaskinn wrote:
>
> On Tue, 02 Mar 2004 02:56:44 -0800, Sathyaish wrote:
>
> > 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 == ' ');
> }
> }
/* 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 */
-- pete
- Next message: nrk: "Re: Please tell me if I've written this correctly"
- Previous message: Jeff: "Re: Is there a difference?"
- In reply to: Nils Petter Vaskinn: "Re: Please tell me if I've written this correctly"
- Next in thread: nrk: "Re: Please tell me if I've written this correctly"
- Reply: nrk: "Re: Please tell me if I've written this correctly"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|