Q: why the array elements stores the value of n-1 instead of n I am trying to do a program which stores the length of each word in an 
array and print it. But the length n isn't printed instead it prints the length n - 1.
#include <stdio.h>

#define IN 1
#define OUT 0
#define MAXLENGTH 10

int main(void)
{
    int i, c, state, word;
    int array[MAXLENGTH];

    state = OUT;
    word = 0;

    for (i = 0; i < MAXLENGTH; i++)
        array[i] = 0;

    while ((c = getchar()) != EOF)
    {
        if (c == '\n' || c == ' ')
            state = OUT;
        else if (state == IN)
        {
            ++array[word];
        }
        else if (state == OUT)
        {
            state = IN;
            ++word;
        }
    }

    for (i = 1; i < MAXLENGTH; i++)
        if (array[i] != 0)
            printf("cuvantu %d are %d caractere \n", i, array[i]);
}


A: Consider this code:
    if (c == '\n' || c == ' ')
        state = OUT;
    else if (state == IN)
    {
        ++array[word];
    }
    else if (state == OUT)
    {
        state = IN;
        ++word;
    }

When c is a new-line or space, it changes the state to OUT, presumably meaning outside of a word.
When c is another character:


*

*If the state is IN, it counts the character by incrementing array[word], which tracks the length of the current word.

*If the state is OUT, it changes the state to IN and increments word to start counting a new word.


In the last case, the current character is not counted—no increment to array[word] is performed. To fix this, insert ++array[word]; in the last case:
    if (c == '\n' || c == ' ')
        state = OUT;
    else if (state == IN)
    {
        ++array[word];
    }
    else if (state == OUT)
    {
        state = IN;
        ++word;
        ++array[word];
    }


A: This seems to be a good exercise to avoid common off-by-one mistakes.
While it could be solved in many ways, the following snippet shows how to modify the logic of the posted state machine so that the array of lengths could be filled from the beginning, without skipping the first element array[0], up to (but not beyond) its maximum size.
#include <stdio.h>

#define IN 1
#define OUT 0
#define MAXLENGTH 16

int main(void)
{
    int array[MAXLENGTH] = {0};

    int c, state = OUT,   
        n_words = 0; //  <-- this name may better convey the purpose of this variable

    while ((c = getchar()) != EOF)
    {
        if (c == '\n' || c == ' ')  // <-- Consider isspace() and iscntrl() instead.
        {
            // We are between words.
            state = OUT;
        }
        else
        {
            // Check the current state before updating it.
            if ( state == OUT )
            {
                // A new word starts, but there might not be enough space.
                if ( n_words == MAXLENGTH )
                {
                    // Here, the rest of the stream is not consumed
                    ungetc(c, stdin);
                    break;     
                }
                ++n_words; 
            }
            state = IN;

            // Array indices are zero based, so the current word is:
            ++array[n_words - 1];
        }
    }

    if ( c != EOF )
        fprintf(stderr, "Sorry, out of space.\n");

    // You can use the actual number of read words to limit the extent of the loop
    printf(" %5s  %5s\n---------------\n", "Word", "Length");
    for (int i = 0; i < n_words; i++)
        printf("%5d  %5d\n", i + 1, array[i]);
}

The code is testable here.
