Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include<string.h>
  7.  
  8. #include<ctype.h>
  9.  
  10.  
  11.  
  12.  
  13.  
  14. //Checking that the lowercase string from a file is same as a lowercase string from another file
  15. int strcmpIgnoreCase(char * one, char * two){
  16.  
  17. int i;
  18. for(i = 0; i < strlen(one); ++i){
  19. one[i] = tolower(one[i]);
  20. }
  21.  
  22. for(i = 0; i < strlen(two); ++i){
  23. two[i] = tolower(two[i]);
  24. }
  25.  
  26.  
  27. /*printf("Comparing words \n");
  28. printf("%s \n", one);
  29. printf("%s \n", two);
  30. */
  31.  
  32. return strcmp(one, two);
  33.  
  34. }
  35.  
  36.  
  37.  
  38.  
  39. char line[1024];
  40.  
  41. //Binary serach the sorted array
  42. int search(char * s1, char ** s2, int dictSize){
  43. int first, last, middle;
  44. first = 0;
  45. last = dictSize - 1;
  46.  
  47.  
  48. while(first <= last)
  49. {
  50. middle = (first + last)/2;
  51. int res = strcmpIgnoreCase(s1, s2[middle]);
  52.  
  53. if(res == 0) {
  54. printf("Correctly spelled");
  55. return middle;
  56.  
  57. }
  58.  
  59. if(res < 0) {
  60. first = middle + 1;
  61. }
  62. else{
  63. last = middle - 1;
  64. }
  65. middle = (first + last)/2;
  66.  
  67. }
  68. return 0;
  69. }
  70.  
  71. //put the dictionary in the the 2 dimensional array where each row would be one word in the dictionary
  72. char** dictionaryData;
  73.  
  74. int idx;
  75.  
  76. //Main Function:
  77. int main(int argc, char **argv){
  78. //File variables
  79. FILE *f1, *f2, *f3;
  80. char *s;
  81. char buffer[1024], text[1024];
  82.  
  83. //Open and read files
  84. f1 = fopen("dictionary.txt", "r");
  85. f2 = fopen("sentences_test.txt", "r");
  86. f3 = fopen("single_words_test.txt", "r");
  87.  
  88. dictionaryData = malloc(45440 * sizeof(char*));
  89.  
  90. int n = 0;
  91.  
  92. while(fgets(buffer, 1024, f1) != NULL){
  93. //printf("%s\n", buffer);
  94.  
  95. dictionaryData[n] = malloc(strlen(buffer)+1 * sizeof(char));
  96. //line[n] = text;
  97. strcpy(dictionaryData[n],buffer);
  98.  
  99. n++;
  100. }
  101.  
  102. /*printf("%d\n",n);
  103. printf("%s\n", dictionaryData[0]);
  104. printf("%s\n", dictionaryData[1]);
  105.  
  106.  
  107. printf("test");*/
  108. //Compare sentences_text file with dictionary file and print out the misspelt words to the new file
  109. while(fgets(text, 1024, f2) != NULL)
  110. {
  111.  
  112. text[strlen(text)] = '\0';
  113.  
  114. idx = search(text, dictionaryData, 45);
  115.  
  116. //Create file
  117. FILE *file;
  118.  
  119. //Open file
  120. file = fopen("misspeltwords.txt", "a");
  121. if(idx == 0){
  122.  
  123. //Write the misspelt word to the file
  124. fprintf(file, "%s\n", text);
  125.  
  126.  
  127. }
  128.  
  129. fclose(file);
  130. }
  131.  
  132.  
  133.  
  134. //Compare single_words_test file with dictionary file and print out the misspelt words to the new file
  135. while(fgets(text, 1024, f3) != NULL);
  136. {
  137. text[strlen(text)]= '\0';
  138. idx = search(text, dictionaryData, 45);
  139.  
  140. //printf("%d",idx);
  141.  
  142. //Create file
  143. FILE *file;
  144.  
  145. //Open file
  146. file = fopen("misspeltwords.txt","a");
  147.  
  148. if(idx == 0){
  149.  
  150.  
  151. //Write the misspelt words to the file
  152. fprintf(file, "%s\n", text);
  153.  
  154.  
  155. }
  156. fclose(file);
  157. }
  158.  
  159. /*int i;
  160. int dictionaryDataLength = strlen(dictionaryLen);
  161. for (i = 0; i <)*/
  162. fclose(f1);
  163. fclose(f2);
  164. fclose(f3);
  165.  
  166. free(dictionaryData);
  167. return 1;
  168.  
  169. }
  170. #include<string.h>
  171.  
  172. #include<ctype.h>
  173.  
  174.  
  175.  
  176.  
  177.  
  178. //Checking that the lowercase string from a file is same as a lowercase string from another file
  179. int strcmpIgnoreCase(char * one, char * two){
  180.  
  181. int i;
  182. for(i = 0; i < strlen(one); ++i){
  183. one[i] = tolower(one[i]);
  184. }
  185.  
  186. for(i = 0; i < strlen(two); ++i){
  187. two[i] = tolower(two[i]);
  188. }
  189.  
  190.  
  191. /*printf("Comparing words \n");
  192. printf("%s \n", one);
  193. printf("%s \n", two);
  194. */
  195.  
  196. return strcmp(one, two);
  197.  
  198. }
  199.  
  200.  
  201.  
  202.  
  203. char line[1024];
  204.  
  205. //Binary serach the sorted array
  206. int search(char * s1, char ** s2, int dictSize){
  207. int first, last, middle;
  208. first = 0;
  209. last = dictSize - 1;
  210.  
  211.  
  212. while(first <= last)
  213. {
  214. middle = (first + last)/2;
  215. int res = strcmpIgnoreCase(s1, s2[middle]);
  216.  
  217. if(res == 0) {
  218. printf("Correctly spelled");
  219. return middle;
  220.  
  221. }
  222.  
  223. if(res < 0) {
  224. first = middle + 1;
  225. }
  226. else{
  227. last = middle - 1;
  228. }
  229. middle = (first + last)/2;
  230.  
  231. }
  232. return 0;
  233. }
  234.  
  235. //put the dictionary in the the 2 dimensional array where each row would be one word in the dictionary
  236. char** dictionaryData;
  237.  
  238. int idx;
  239.  
  240. //Main Function:
  241. int main(int argc, char **argv){
  242. //File variables
  243. FILE *f1, *f2, *f3;
  244. char *s;
  245. char buffer[1024], text[1024];
  246.  
  247. //Open and read files
  248. f1 = fopen("dictionary.txt", "r");
  249. f2 = fopen("sentences_test.txt", "r");
  250. f3 = fopen("single_words_test.txt", "r");
  251.  
  252. dictionaryData = malloc(45440 * sizeof(char*));
  253.  
  254. int n = 0;
  255.  
  256. while(fgets(buffer, 1024, f1) != NULL){
  257. //printf("%s\n", buffer);
  258.  
  259. dictionaryData[n] = malloc(strlen(buffer)+1 * sizeof(char));
  260. //line[n] = text;
  261. strcpy(dictionaryData[n],buffer);
  262.  
  263. n++;
  264. }
  265.  
  266. /*printf("%d\n",n);
  267. printf("%s\n", dictionaryData[0]);
  268. printf("%s\n", dictionaryData[1]);
  269.  
  270.  
  271. printf("test");*/
  272. //Compare sentences_text file with dictionary file and print out the misspelt words to the new file
  273. while(fgets(text, 1024, f2) != NULL)
  274. {
  275.  
  276. text[strlen(text)] = '\0';
  277.  
  278. idx = search(text, dictionaryData, 45);
  279.  
  280. //Create file
  281. FILE *file;
  282.  
  283. //Open file
  284. file = fopen("misspeltwords.txt", "a");
  285. if(idx == 0){
  286.  
  287. //Write the misspelt word to the file
  288. fprintf(file, "%s\n", text);
  289.  
  290.  
  291. }
  292.  
  293. fclose(file);
  294. }
  295.  
  296.  
  297.  
  298. //Compare single_words_test file with dictionary file and print out the misspelt words to the new file
  299. while(fgets(text, 1024, f3) != NULL);
  300. {
  301. text[strlen(text)]= '\0';
  302. idx = search(text, dictionaryData, 45);
  303.  
  304. //printf("%d",idx);
  305.  
  306. //Create file
  307. FILE *file;
  308.  
  309. //Open file
  310. file = fopen("misspeltwords.txt","a");
  311.  
  312. if(idx == 0){
  313.  
  314.  
  315. //Write the misspelt words to the file
  316. fprintf(file, "%s\n", text);
  317.  
  318.  
  319. }
  320. fclose(file);
  321. }
  322.  
  323. /*int i;
  324. int dictionaryDataLength = strlen(dictionaryLen);
  325. for (i = 0; i <)*/
  326. fclose(f1);
  327. fclose(f2);
  328. fclose(f3);
  329.  
  330. free(dictionaryData);
  331. return 1;
  332.  
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement