int main(int argc, char **argv) { list_node *hashtable[HASHTABLE_SIZE]; init_hashtable(hashtable); // Determining the file size struct stat st; if(stat("names.txt", &st) == -1) return EXIT_FAILURE; // Reading all contents to memory char contents[st.st_size + 1]; FILE *h = fopen("names.txt", "r"); size_t n = fread(contents, sizeof(char), st.st_size, h); fclose(h); // Extracting names and inserting in hashtable int j; char aux[NAME_LENGTH+1]; for(int i = 0; i < n; i++) { bzero(aux, NAME_LENGTH); j = 0; while(contents[i] != ',' && i < n) { if(contents[i] != '"') aux[j++] = contents[i]; i++; } aux[j] = '\0'; insert_name(hashtable, aux, strlen(aux)); } // Reading names long total = 0; int pos = 1; list_node *aux_node; for(int i = 0; i < HASHTABLE_SIZE; i++) { if(hashtable[i] != NULL) { aux_node = hashtable[i]; while(aux_node != NULL) { total += name_score( aux_node->name, strlen(aux_node->name), pos++); aux_node = aux_node->next; } } } printf("%ld\n", total); return EXIT_SUCCESS; }
int main() { int position = 0, i; char **names; char buffer[FILESIZE]; const char separator[2] = ","; char *token; unsigned long total = 0; // allocate memory for 6000 words names = malloc(6000 * sizeof(char *)); while (fgets(buffer, FILESIZE * sizeof(char), stdin)) { token = strtok(buffer, separator); while (token != NULL) { // remove last quote by shortening token by one token[strlen(token) - 1] = 0; // disregard first quote by passing string starting on position 1 names[position++] = &token[1]; token = strtok(NULL, separator); } } // SORT SHIT OUT qsort(names, position, sizeof(char *), sortstring); for (i = 0; i < position; i++) { total += name_score(names[i], i + 1); } printf("%lu", total); return 0; }