Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- clock_t start, end;
- double cpu_time_used;
- //static int cmp(const void *a, const void *b) { return *(char *)a - *(char *)b; }
- // for pt2
- int main(void)
- {
- start = clock();
- // open in the password file and check for errors
- FILE *fp;
- fp = fopen("pass.txt", "r");
- if(fp == NULL)
- {
- printf("Failed to open password file\n");
- exit(1); // exit with error if file not found
- }
- char string[100]; // string to read line in
- char *tokenPtr; // pointer to token
- char **arr; // pointer to array of strings
- int i = 0; // iterator
- int tokens = 1; // number of tokens
- int check = 0; // has it failed
- int count = 0; // number of valid passwords
- while(fgets(string,100,fp)) // read till eof
- {
- // read string in from the file
- //printf("%s", string); // print it
- for(i = 0; string[i] != '\0'; i++) // find the amount of tokens in the string
- {
- if(string[i] == ' ')
- {
- tokens++;
- }
- }
- string[i-1] = '\0'; // remove the \n
- //printf("done %d strings found\n", tokens);
- tokenPtr = strtok(string, " "); // tokenize the string
- arr = malloc(tokens * sizeof(char*)); // malloc array for number of strings
- for(i = 0; i < tokens; i++) // malloc a array for each string
- { // arr is an array of pointers to strings
- // for pt2 qsort(tokenPtr, strlen(tokenPtr), 1, (cmp));
- arr[i] = malloc(20 * sizeof(char));
- strcpy(arr[i], tokenPtr); // copy string into the newly allocated array
- tokenPtr = strtok(NULL, " ");
- }
- check = 0; // reset the check
- for(int j = 0; j < tokens; j++)
- {
- for(i = 0; i < tokens; i++)
- {
- if(strcmp(arr[j], arr[i]) == 0 && i != j)
- {
- //printf("strings are the same\n");
- check = 1; // check failed
- }
- }
- }
- if(check != 1) // if no strings were the same count passed increment it
- {
- count++;
- }
- for(i = 0; i < tokens; i++) // free the strings
- {
- free(arr[i]);
- }
- tokens = 1; // reset the ammount of strings
- }
- printf("valid passwords: %d\n", count);
- end = clock();
- double time_taken = ((double) (end - start)) / CLOCKS_PER_SEC;
- printf("program took %f seconds to execute\n", time_taken);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment