Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include<string.h>
  5.  
  6. #include<ctype.h>
  7.  
  8.  
  9.  
  10.  
  11.  
  12. //Checking that the lowercase string from a file is same as a lowercase string from another file
  13. int strcmpIgnoreCase(char * one, char * two){
  14.  
  15. int i;
  16. for(i = 0; i < strlen(one); ++i){
  17. one[i] = tolower(one[i]);
  18. }
  19.  
  20. for(i = 0; i < strlen(two); ++i){
  21. two[i] = tolower(two[i]);
  22. }
  23.  
  24.  
  25. /*printf("Comparing words \n");
  26. printf("%s \n", one);
  27. printf("%s \n", two);
  28. */
  29.  
  30. return strcmp(one, two);
  31.  
  32. }
  33.  
  34.  
  35.  
  36.  
  37. char line[1024];
  38.  
  39. //Binary serach the sorted array
  40. int search(char * s1, char ** s2, int dictSize){
  41. int first, last, middle;
  42. first = 0;
  43. last = dictSize - 1;
  44.  
  45.  
  46. while(first <= last)
  47. {
  48. middle = (first + last)/2;
  49. int res = strcmpIgnoreCase(s1, s2[middle]);
  50.  
  51. if(res == 0) {
  52. printf("Correctly spelled");
  53. return middle;
  54.  
  55. }
  56.  
  57. if(res < 0) {
  58. first = middle + 1;
  59. }
  60. else{
  61. last = middle - 1;
  62. }
  63. middle = (first + last)/2;
  64.  
  65. }
  66. return 0;
  67. }
  68.  
  69. //put the dictionary in the the 2 dimensional array where each row would be one word in the dictionary
  70. char** dictionaryData;
  71.  
  72. int idx;
  73.  
  74. //Main Function:
  75. int main(int argc, char **argv){
  76. //File variables
  77. FILE *f1, *f2, *f3;
  78. char *s;
  79. char buffer[1024], text[1024];
  80.  
  81. //Open and read files
  82. f1 = fopen("dictionary.txt", "r");
  83. f2 = fopen("sentences_test.txt", "r");
  84. f3 = fopen("single_words_test.txt", "r");
  85.  
  86. dictionaryData = malloc(45440 * sizeof(char*));
  87.  
  88. int n = 0;
  89.  
  90. while(fgets(buffer, 1024, f1) != NULL){
  91. //printf("%s\n", buffer);
  92.  
  93. dictionaryData[n] = malloc(strlen(buffer)+1 * sizeof(char));
  94. //line[n] = text;
  95. strcpy(dictionaryData[n],buffer);
  96.  
  97. n++;
  98. }
  99.  
  100. /*printf("%d\n",n);
  101. printf("%s\n", dictionaryData[0]);
  102. printf("%s\n", dictionaryData[1]);
  103.  
  104.  
  105. printf("test");*/
  106. //Compare sentences_text file with dictionary file and print out the misspelt words to the new file
  107. while(fgets(text, 1024, f2) != NULL)
  108. {
  109.  
  110. text[strlen(text)] = '\0';
  111.  
  112. idx = search(text, dictionaryData, 45);
  113.  
  114. //Create file
  115. FILE *file;
  116.  
  117. //Open file
  118. file = fopen("misspeltwords.txt", "a");
  119. if(idx == 0){
  120.  
  121. //Write the misspelt word to the file
  122. fprintf(file, "%s\n", text);
  123.  
  124.  
  125. }
  126.  
  127. fclose(file);
  128. }
  129.  
  130.  
  131.  
  132. //Compare single_words_test file with dictionary file and print out the misspelt words to the new file
  133. while(fgets(text, 1024, f3) != NULL);
  134. {
  135. text[strlen(text)]= '\0';
  136. idx = search(text, dictionaryData, 45);
  137.  
  138. //printf("%d",idx);
  139.  
  140. //Create file
  141. FILE *file;
  142.  
  143. //Open file
  144. file = fopen("misspeltwords.txt","a");
  145.  
  146. if(idx == 0){
  147.  
  148.  
  149. //Write the misspelt words to the file
  150. fprintf(file, "%s\n", text);
  151.  
  152.  
  153. }
  154. fclose(file);
  155. }
  156.  
  157. /*int i;
  158. int dictionaryDataLength = strlen(dictionaryLen);
  159. for (i = 0; i <)*/
  160. fclose(f1);
  161. fclose(f2);
  162. fclose(f3);
  163.  
  164. free(dictionaryData);
  165. return 1;
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement