pot value

char string[4]; //can be 4, 8 is fine

You are now sending up to 5 characters plus there needs to be room for the NULL.

        string[index]=var;
        index++;
        string[index]='\0';
        string[index]=var; //store character

You are stomping on the NULL. You have already stored the character.

    val=atoi(string);

Because you are stomping on the NULL, the array string is NOT a string, so it is not valid input to atoi().