#include	<stdio.h>
#include	<ctype.h>
#include    <string.h>

/*
 *  q6.c
 *
 *	convert input file and output those fields that obviously have bad data
 *
 */ 

#define	LINESIZE	     		512
#define MAXFIELDS               100
#define MAXFLDLEN               50
#define MAXDELIM                10


main()
{

	char    line[LINESIZE];                                // an array of characters
	char    record[MAXFIELDS][MAXFLDLEN];                  // the record	
	int 	itemDelimeters[MAXDELIM] = {' ', '\t', '\n'};  // item delimeters
	int  	i, number_of_fields;

	
	
    // loop through each line of data 
    while ( fgets( line, LINESIZE, stdin) != NULL ) {
		
		// split line into record
		number_of_fields = parse_line(itemDelimeters, line, record );

		// loop through each record
		for ( i = 0 ; i < number_of_fields ; i++ ) {
			printf("%s \n", record[i]);     
		}
		printf("numOfFields %d \n", number_of_fields);	
	}

	
}



int
parse_line(int itemDel[], char mString[], char fields[MAXFIELDS][MAXFLDLEN]) 
//
// parse mString into an array of separate fields
//
// uses a list of item delimiters to determine breaks
//  method: loop through mString copying and copy to target array.  Change
//          to next row at each colon
//    note: no error checking.  See what you can do to break it
//
	
{
		int     i           = 0,		// general purpose counter
				endFlag     = 0,        // flag for indicating record is finished
				readAt      = -1,       // reading position in source array
                currentFld  = 0,        // item in fields[] array
				destination = 0;        // destination in current field				
		
        do {
                // loop through mString
			
				readAt++;
				
				// error check for readAt hitting maximum or end of mString
				if ( readAt == MAXFLDLEN || mString[readAt]  == NULL )
					endFlag = 1;            // flag record as ended
				
				// loop through each possible delimiter
				for (i = 0; itemDel[i] != NULL && ! endFlag ; i++) {
					
					if ( mString[readAt] == itemDel[i] )
						endFlag = 1;		// flag record as ended
				}

				// 

                if ( ! endFlag ) {
						//
                 		// not end of record, copy the char
					
                        fields[currentFld][destination] = mString[readAt];
                        destination++;
                }
                else {
						//
						// end record, terminate this field
					
						fields[currentFld][destination] = '\0';    

						// check if field is empty before incrementing
						if ( fields[currentFld][0] != '\0') {
                        	currentFld++;
						}

						// reset for new field
						destination = 0;
						endFlag = 0;
                }
				
        } while ( mString[readAt] != NULL );
		
		// return number of fields found in line
        return currentFld;
}
