/*
 * File:   main.c
* Author: Joe Churchwell
*
* Created on October 21, 2014, 9:27 PM
*/
 
#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 1024
#define MAX_RESULTS 100
#define CAP_A       65
#define CAP_Z       90
#define LOW_A       97
#define LOW_Z       122
#define PERIOD      46
#define UNDER_BAR   95
#define ZERO        48
#define NINE        57
#define PASS        0

#define FAILED_NUMBER_AFTER_LETTER 1
#define FAILED_NOT_A_LETTER_AFTER_NUMBER 2
#define FAILED_NOT_A_NUMBER_AT_START 3
#define FAILED_INVALID_START_CHAR 4

/*
*  This is a c program to determine a valid alien user name
 * ref: https://www.hackerrank.com/challenges/alien-username
 * This is strait code which does not use regex
 * defines are in this file because of no known way to include a header
 * file on the web submission
*/
int main(int argc, char** argv) {
 
    unsigned char str_buffer[BUFFER_SIZE];
    unsigned char * input;
    unsigned char results[MAX_RESULTS];
              
    int user_name_count;
    unsigned int i;
    unsigned char past_numeric = 0;
    unsigned int failed;
       
    // First get the number of users to receive     
    fgets(str_buffer, MAX_RESULTS, stdin);
                             
    user_name_count = atoi(str_buffer);    
              
    // Make sure there are no more than the max number of user names to verify
    // Also make sure that we do not have a negative value or no users
    if (user_name_count > MAX_RESULTS || user_name_count < 1){
        printf("ERROR-Usage: Must be more than 0 and less than 100 users names");
        return(EXIT_FAILURE);
    }                        
   
    // Loop through all the desired usernames
    for(i=1; i < user_name_count+1; i++)
    {
        failed = PASS; // re-set the failed indicator
       
        // Get the line from the standard input
        fgets(str_buffer, BUFFER_SIZE, stdin);  
        input = str_buffer;
       
        // Verify the username starts with the proper format
        if(*input == UNDER_BAR || *input == PERIOD) // "_" and "."
        {               
            input++; // Increment the char pointer to get next char
            // Check to make sure it starts with a number after the start char               
            if(*input < NINE+1 && *input > ZERO-1){
                // Now check for the rest of the valid format
                while(*(++input) > ZERO-1){                                                
                    if(*input < NINE+1 && *input > ZERO-1){
                        if(past_numeric){
                            failed = FAILED_NUMBER_AFTER_LETTER;
                            break;
                        }
                    }else{ // This is not a number
                        past_numeric = 1;
                        if((*input > LOW_A-1 && *input < LOW_Z+1) ||
                           (*input > CAP_A-1 && *input < CAP_Z+1)  ||
                           (*input == UNDER_BAR && *(input+1) < ZERO)
                          ){
                            continue;
                        }else{
                            failed = FAILED_NOT_A_LETTER_AFTER_NUMBER;
                            break;
                        }                                                           
                    }
                } //  END FOR
                results[i-1] = failed;                                                                     
            }else{ // This was not a valid number                   
                results[i-1] = FAILED_NOT_A_NUMBER_AT_START;
            }                               
        }else{ // The user name did not start with a '.' or '_'
           results[i-1] = FAILED_INVALID_START_CHAR;
        }                                                          
    } // END FOR
              
    // Loop through the results and print them to stdout
    for(i=0; i < user_name_count; i++)
    {         
        if(results[i]){
            printf("INVALID\n");
        }else{
            printf("VALID\n");
        }                                           
    }
    
    return (EXIT_SUCCESS);
}