Home > Mobile >  why the second space, statement count[length] ; not executed but switch to length = 0 statement?
why the second space, statement count[length] ; not executed but switch to length = 0 statement?

Time:10-09

my input is "Hoang...Dung'' (I change three space as ...) after i debug i noticed that at the first space, state = 1, length = 0, max = 5 why the second space, statement count[length] ; not executed but switch to length = 0 statement?


#define IN  0
#define OUT 1

main()
{
    int state;
    int length, max;
    int count[8];
    int i,  j;
char c;
    length = 0;
    max = 0;
    state = OUT;

    for (i = 0; i < 8;   i)
        count[i] = 0;

    while ((c = getchar()) != EOF) {
        if (c == ' ' || c == '\n' || c == '\t') {
            state = OUT;

            if (length > max)
                max = length;

            if (length != 0)
                count[length]  ;

            length = 0;
        } else if (state == OUT) {
            state = IN;
            length  ;
        } else {
            length  ;
        }
    }

    for (i = 0; i < max   1;   i) {
        printf("-: ", i);
        for (j = 0; j < count[i];     j) {
            putchar('#');
        }
        printf("\n");
    }

}

CodePudding user response:

its because switch to length=0 isn't bound by any condition, i am not getting what you are trying to achieve in your question, but put brackets after every if/else condition to better understand and to debug print output wherever you feel you are going wrong.

  •  Tags:  
  • c
  • Related