Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Header
- Command: ./a.out dictionaryFileName -lnn -tmmm
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- FILE * dictFile;
- int main(int argc, char * argv[]) {
- char tempString[50];
- char * dictionary;
- int * positions;
- int n = 0, m = 0;
- int wordCount = 0;
- int charCount = 0;
- positions = (int * ) malloc(50 * sizeof(int));
- dictionary = (char * ) malloc(500 * sizeof(char));
- int posReallocCount = 1;
- int dictReallocCount = 1;
- int wordStartPos = 0;
- // Vars for randomized string
- char randString[100];
- int randCharCount = 0;
- srand(time(NULL));
- //Vars for sorting random string
- int charWeights[26] = {
- 0
- };
- int tempRandStorage;
- //Var for sorting dictionary
- int pointerToChar = 0;
- int dictWeights[26] = {
- 0
- };
- char upperCase;
- //Vars for comparing strings:
- int weightChanger = 0;
- //Vars for Outputting strings
- int * outPosition;
- int * outLength;
- char * outputString;
- void * temp;
- outPosition = (int * ) malloc(50 * sizeof(int));
- outLength = (int * ) malloc(50 * sizeof(int));
- outputString = (char * ) malloc(500 * sizeof(char));
- int outPosReallocCount = 1;
- int outStrReallocCount = 1;
- int outputCounter = 0;
- int outPos = 0;
- int numTimes = 1;
- int numRandom = 0;
- int timesCounter = 0;
- //Vars for ArgV ArgC
- int i = 1;
- char argumentJunk[10];
- while (i < argc) {
- /* ANALYZE ARGV */
- if (strncmp(argv[i], "-t", 2) == 0) {
- sscanf(argv[i], "%2s%i", argumentJunk, & numTimes);
- if (numTimes < 0 || numTimes > 999) {
- printf("Error, Outside Range.\nNumber of times must be between 1 to 999: -tmmm\n");
- return 1;
- }
- } else if (strncmp(argv[i], "-l", 2) == 0) {
- sscanf(argv[i], "%2s%i", argumentJunk, & numRandom);
- if (numRandom < 0 || numRandom > 99) {
- printf("Error, Outside Range.\nNumber of random letters must be between 1 to 99: -lnn\n");
- return 1;
- }
- } else {
- dictFile = fopen(argv[i], "r");
- }
- i++;
- }
- while (timesCounter < numTimes) {
- /*/* Set Vars Every Loop */
- /*free(positions);
- free(dictionary);
- free(outPosition);
- free(outLength);
- free(outputString);
- */
- temp = (int * ) realloc(positions, 50 * sizeof(int));
- if (temp) positions = temp;
- temp = (char * ) realloc(dictionary, 500 * sizeof(char));
- if (temp) dictionary = temp;
- temp = (int * ) realloc(outPosition, 50 * sizeof(int));
- if (temp) outPosition = temp;
- temp = (int * ) realloc(outLength, 50 * sizeof(int));
- if (temp) outLength = temp;
- temp = (char * ) realloc(outputString, 500 * sizeof(char));
- if (temp) outputString = temp;
- /*
- n=0,m=0;
- wordCount=0;
- charCount=0;
- posReallocCount=1;
- dictReallocCount=1;
- wordStartPos=0;
- // Vars for randomized string
- randCharCount=0;
- //Vars for sorting random string
- */
- //Var for sorting dictionary
- /*pointerToChar=0;
- //Vars for comparing strings:
- weightChanger=0;
- outPosReallocCount=1;
- outStrReallocCount=1;
- outputCounter=0;
- outPosition=0;
- outPos=0;
- timesCounter=0;
- */
- if (numRandom == 0) {
- printf("Enter between 1 and 99 characters you want to search: ");
- scanf("%s", randString);
- numRandom = strlen(randString);
- i = 0;
- while (i < numRandom) {
- tempRandStorage = toupper(randString[i]) - 65;
- charWeights[tempRandStorage]++;
- i++;
- }
- } else {
- /* RANDOMIZE STRING */
- randString[numRandom] = '\0';
- while (randCharCount < numRandom) {
- tempRandStorage = 65 + rand() % 26;
- charWeights[tempRandStorage - 65]++;
- randString[randCharCount] = tempRandStorage;
- randCharCount++;
- }
- }
- /* SCAN FILE ALLOCATE MEMORY */
- while (fgets(tempString, 49, dictFile) != NULL) {
- ++wordCount;
- //scan in tempstring and find length
- charCount = strlen(tempString);
- //Realloc arrays
- while (wordCount / 50 > posReallocCount - 1) {
- ++posReallocCount;
- positions = (int * ) realloc(positions, 50 * posReallocCount * sizeof(int));
- }
- positions[wordCount - 1] = wordStartPos;
- wordStartPos += charCount;
- wordStartPos++;
- while (wordStartPos / 500 > dictReallocCount - 1) {
- ++dictReallocCount;
- dictionary = (char * ) realloc(dictionary, 500 * dictReallocCount * sizeof(char));
- }
- //copy tempstring to dictionary
- strncpy(dictionary + wordStartPos - charCount - 1, tempString, charCount);
- dictionary[wordStartPos - 1] = '\0';
- dictionary[wordStartPos] = '\0';
- }
- fclose(dictFile);
- /* END INITIALIZATION */
- /* BEGIN WORD SEARCH */
- n = 0;
- //memset(charWeights,0,26*sizeof(int));
- while (n < wordCount) {
- //Reset weights and pointer to the character
- memset(dictWeights, 0, 26 * sizeof(int));
- pointerToChar = 0;
- //Convert string to character weights
- while (pointerToChar < positions[n + 1] - positions[n]) {
- upperCase = toupper(dictionary[positions[n] + pointerToChar]);
- if (upperCase < 91 && upperCase > 64) {
- dictWeights[upperCase - 65]++;
- }
- pointerToChar++;
- }
- weightChanger = 0;
- // Check weights from dictionary to the string of characters
- while ((dictWeights[weightChanger] <= charWeights[weightChanger]) && weightChanger < 26) {
- ++weightChanger;
- }
- // If match create output
- if (weightChanger == 26) {
- /* CREATE OUTPUT DATA */
- //realloc arrays
- if (((outPosition[outputCounter] + 100) % 1000) >= 500) {
- ++outStrReallocCount;
- outputString = (char * ) realloc(outputString, 500 * outStrReallocCount * sizeof(char));
- }
- if (((outputCounter + 1) % 50) == 0) {
- ++outPosReallocCount;
- outLength = (int * ) realloc(outLength, 50 * outPosReallocCount * sizeof(int));
- outPosition = (int * ) realloc(outPosition, 50 * outPosReallocCount * sizeof(int));
- }
- //Update arrays with new output data
- outLength[outputCounter] = positions[n + 1] - positions[n];
- outputString[outPos] = '\0';
- outputString[outPos - 1] = '\0';
- strncat(outputString + outPos, dictionary + positions[n], outLength[outputCounter]);
- outPos += outLength[outputCounter];
- outPosition[outputCounter + 1] = outPos;
- outputCounter++;
- }
- n++;
- }
- /* FINAL PRINT STATEMENT */
- printf("Checking the letters: \"%s\"\n", randString);
- printf("<<< Run %i >>>\n", timesCounter + 1);
- if (wordCount == 0) {
- printf("No words were found. Try another dictionary.\n");
- } else {
- m = 3;
- while (m < numRandom + 3) {
- n = 0;
- //print strings
- printf("Results for %i Letters:\n", m - 2);
- while (n < outputCounter) {
- if (outLength[n] == m) {
- printf("%s", outputString + outPosition[n]);
- }
- n++;
- }
- m++;
- }
- }
- timesCounter++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement