Re: Please tell me if I've written this correctly

From: pete (pfiland_at_mindspring.com)
Date: 03/02/04


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


Relevant Pages

  • Re: Essential Features
    ... already is bitwise AND on integers. ... unsigned int corrections = ... if (NULL == (fgets(buff, sizeof buff, stdin))) ... printf("Original value 0x%02X Hamming Code 0x%X\n", ...
    (comp.lang.fortran)
  • Re: how to set default values in instantiating a generic List<>
    ... int, it seems clear that this is actually bitwise data... ... but with proper handling of the enum as a bitwise entity. ... I know that this doesn't answer your question with lists, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: exponential identity seed
    ... > are an int and a varchar. ... > This way I can store and manage the selected preferences using bitwise ... > To avoid problems I would like to auto-generate this exponential sequence ...
    (microsoft.public.sqlserver.programming)
  • Re: Auditing C code
    ... know or care about the difference between int, int16_t, unsigned int, ... He does a lot of bitwise |, &, etc on signed int, without ... parameters, to make sure that all passed parameters are of the same type, ...
    (comp.lang.c)
  • Re: (i++ * i++)
    ... Nils Petter Vaskinn wrote: ... trivially modified to account for any conceivable intent of the original ... : int i = 5; ... Still undefined behavior. ...
    (comp.lang.cpp)