Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. /*Header
  2.  
  3. Command: ./a.out dictionaryFileName -lnn -tmmm
  4.  
  5.  
  6.  
  7. */
  8.  
  9. #include <stdio.h>
  10.  
  11. #include <stdlib.h>
  12.  
  13. #include <string.h>
  14.  
  15. #include <time.h>
  16.  
  17. #include <ctype.h>
  18.  
  19. FILE * dictFile;
  20. int main(int argc, char * argv[]) {
  21.  
  22. char tempString[50];
  23. char * dictionary;
  24. int * positions;
  25. int n = 0, m = 0;
  26. int wordCount = 0;
  27. int charCount = 0;
  28. positions = (int * ) malloc(50 * sizeof(int));
  29. dictionary = (char * ) malloc(500 * sizeof(char));
  30.  
  31. int posReallocCount = 1;
  32. int dictReallocCount = 1;
  33. int wordStartPos = 0;
  34. // Vars for randomized string
  35. char randString[100];
  36. int randCharCount = 0;
  37. srand(time(NULL));
  38. //Vars for sorting random string
  39. int charWeights[26] = {
  40. 0
  41. };
  42. int tempRandStorage;
  43.  
  44. //Var for sorting dictionary
  45. int pointerToChar = 0;
  46. int dictWeights[26] = {
  47. 0
  48. };
  49. char upperCase;
  50. //Vars for comparing strings:
  51. int weightChanger = 0;
  52. //Vars for Outputting strings
  53. int * outPosition;
  54. int * outLength;
  55. char * outputString;
  56. void * temp;
  57. outPosition = (int * ) malloc(50 * sizeof(int));
  58. outLength = (int * ) malloc(50 * sizeof(int));
  59. outputString = (char * ) malloc(500 * sizeof(char));
  60. int outPosReallocCount = 1;
  61. int outStrReallocCount = 1;
  62.  
  63. int outputCounter = 0;
  64.  
  65. int outPos = 0;
  66. int numTimes = 1;
  67. int numRandom = 0;
  68. int timesCounter = 0;
  69. //Vars for ArgV ArgC
  70. int i = 1;
  71. char argumentJunk[10];
  72.  
  73. while (i < argc) {
  74.  
  75. /* ANALYZE ARGV */
  76. if (strncmp(argv[i], "-t", 2) == 0) {
  77. sscanf(argv[i], "%2s%i", argumentJunk, & numTimes);
  78. if (numTimes < 0 || numTimes > 999) {
  79. printf("Error, Outside Range.\nNumber of times must be between 1 to 999: -tmmm\n");
  80. return 1;
  81. }
  82. } else if (strncmp(argv[i], "-l", 2) == 0) {
  83. sscanf(argv[i], "%2s%i", argumentJunk, & numRandom);
  84.  
  85. if (numRandom < 0 || numRandom > 99) {
  86.  
  87. printf("Error, Outside Range.\nNumber of random letters must be between 1 to 99: -lnn\n");
  88. return 1;
  89. }
  90. } else {
  91. dictFile = fopen(argv[i], "r");
  92. }
  93.  
  94. i++;
  95. }
  96.  
  97. while (timesCounter < numTimes) {
  98. /*/* Set Vars Every Loop */
  99. /*free(positions);
  100. free(dictionary);
  101. free(outPosition);
  102. free(outLength);
  103. free(outputString);
  104. */
  105. temp = (int * ) realloc(positions, 50 * sizeof(int));
  106. if (temp) positions = temp;
  107. temp = (char * ) realloc(dictionary, 500 * sizeof(char));
  108. if (temp) dictionary = temp;
  109. temp = (int * ) realloc(outPosition, 50 * sizeof(int));
  110. if (temp) outPosition = temp;
  111. temp = (int * ) realloc(outLength, 50 * sizeof(int));
  112. if (temp) outLength = temp;
  113. temp = (char * ) realloc(outputString, 500 * sizeof(char));
  114. if (temp) outputString = temp;
  115.  
  116. /*
  117. n=0,m=0;
  118. wordCount=0;
  119. charCount=0;
  120.  
  121. posReallocCount=1;
  122. dictReallocCount=1;
  123. wordStartPos=0;
  124. // Vars for randomized string
  125. randCharCount=0;
  126.  
  127. //Vars for sorting random string
  128.  
  129.  
  130. */
  131. //Var for sorting dictionary
  132. /*pointerToChar=0;
  133.  
  134.  
  135. //Vars for comparing strings:
  136. weightChanger=0;
  137.  
  138. outPosReallocCount=1;
  139. outStrReallocCount=1;
  140.  
  141. outputCounter=0;
  142. outPosition=0;
  143. outPos=0;
  144.  
  145. timesCounter=0;
  146.  
  147. */
  148.  
  149. if (numRandom == 0) {
  150. printf("Enter between 1 and 99 characters you want to search: ");
  151. scanf("%s", randString);
  152. numRandom = strlen(randString);
  153. i = 0;
  154. while (i < numRandom) {
  155. tempRandStorage = toupper(randString[i]) - 65;
  156. charWeights[tempRandStorage]++;
  157. i++;
  158. }
  159. } else {
  160. /* RANDOMIZE STRING */
  161. randString[numRandom] = '\0';
  162. while (randCharCount < numRandom) {
  163. tempRandStorage = 65 + rand() % 26;
  164. charWeights[tempRandStorage - 65]++;
  165. randString[randCharCount] = tempRandStorage;
  166. randCharCount++;
  167. }
  168.  
  169. }
  170. /* SCAN FILE ALLOCATE MEMORY */
  171.  
  172. while (fgets(tempString, 49, dictFile) != NULL) {
  173. ++wordCount;
  174. //scan in tempstring and find length
  175. charCount = strlen(tempString);
  176. //Realloc arrays
  177. while (wordCount / 50 > posReallocCount - 1) {
  178. ++posReallocCount;
  179. positions = (int * ) realloc(positions, 50 * posReallocCount * sizeof(int));
  180. }
  181. positions[wordCount - 1] = wordStartPos;
  182. wordStartPos += charCount;
  183. wordStartPos++;
  184.  
  185. while (wordStartPos / 500 > dictReallocCount - 1) {
  186. ++dictReallocCount;
  187. dictionary = (char * ) realloc(dictionary, 500 * dictReallocCount * sizeof(char));
  188.  
  189. }
  190. //copy tempstring to dictionary
  191. strncpy(dictionary + wordStartPos - charCount - 1, tempString, charCount);
  192. dictionary[wordStartPos - 1] = '\0';
  193. dictionary[wordStartPos] = '\0';
  194. }
  195. fclose(dictFile);
  196.  
  197. /* END INITIALIZATION */
  198.  
  199. /* BEGIN WORD SEARCH */
  200. n = 0;
  201. //memset(charWeights,0,26*sizeof(int));
  202. while (n < wordCount) {
  203. //Reset weights and pointer to the character
  204. memset(dictWeights, 0, 26 * sizeof(int));
  205. pointerToChar = 0;
  206. //Convert string to character weights
  207. while (pointerToChar < positions[n + 1] - positions[n]) {
  208. upperCase = toupper(dictionary[positions[n] + pointerToChar]);
  209. if (upperCase < 91 && upperCase > 64) {
  210. dictWeights[upperCase - 65]++;
  211. }
  212. pointerToChar++;
  213. }
  214. weightChanger = 0;
  215. // Check weights from dictionary to the string of characters
  216. while ((dictWeights[weightChanger] <= charWeights[weightChanger]) && weightChanger < 26) {
  217. ++weightChanger;
  218. }
  219. // If match create output
  220. if (weightChanger == 26) {
  221. /* CREATE OUTPUT DATA */
  222.  
  223. //realloc arrays
  224. if (((outPosition[outputCounter] + 100) % 1000) >= 500) {
  225. ++outStrReallocCount;
  226. outputString = (char * ) realloc(outputString, 500 * outStrReallocCount * sizeof(char));
  227. }
  228. if (((outputCounter + 1) % 50) == 0) {
  229. ++outPosReallocCount;
  230. outLength = (int * ) realloc(outLength, 50 * outPosReallocCount * sizeof(int));
  231. outPosition = (int * ) realloc(outPosition, 50 * outPosReallocCount * sizeof(int));
  232. }
  233. //Update arrays with new output data
  234. outLength[outputCounter] = positions[n + 1] - positions[n];
  235. outputString[outPos] = '\0';
  236. outputString[outPos - 1] = '\0';
  237. strncat(outputString + outPos, dictionary + positions[n], outLength[outputCounter]);
  238. outPos += outLength[outputCounter];
  239. outPosition[outputCounter + 1] = outPos;
  240. outputCounter++;
  241. }
  242. n++;
  243. }
  244. /* FINAL PRINT STATEMENT */
  245. printf("Checking the letters: \"%s\"\n", randString);
  246. printf("<<< Run %i >>>\n", timesCounter + 1);
  247. if (wordCount == 0) {
  248. printf("No words were found. Try another dictionary.\n");
  249. } else {
  250. m = 3;
  251. while (m < numRandom + 3) {
  252. n = 0;
  253. //print strings
  254. printf("Results for %i Letters:\n", m - 2);
  255. while (n < outputCounter) {
  256. if (outLength[n] == m) {
  257. printf("%s", outputString + outPosition[n]);
  258. }
  259. n++;
  260. }
  261. m++;
  262. }
  263. }
  264.  
  265. timesCounter++;
  266.  
  267. }
  268. return 0;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement