Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include<string.h>
- #include<ctype.h>
- //Checking that the lowercase string from a file is same as a lowercase string from another file
- int strcmpIgnoreCase(char * one, char * two){
- int i;
- for(i = 0; i < strlen(one); ++i){
- one[i] = tolower(one[i]);
- }
- for(i = 0; i < strlen(two); ++i){
- two[i] = tolower(two[i]);
- }
- /*printf("Comparing words \n");
- printf("%s \n", one);
- printf("%s \n", two);
- */
- return strcmp(one, two);
- }
- char line[1024];
- //Binary serach the sorted array
- int search(char * s1, char ** s2, int dictSize){
- int first, last, middle;
- first = 0;
- last = dictSize - 1;
- while(first <= last)
- {
- middle = (first + last)/2;
- int res = strcmpIgnoreCase(s1, s2[middle]);
- if(res == 0) {
- printf("Correctly spelled");
- return middle;
- }
- if(res < 0) {
- first = middle + 1;
- }
- else{
- last = middle - 1;
- }
- middle = (first + last)/2;
- }
- return 0;
- }
- //put the dictionary in the the 2 dimensional array where each row would be one word in the dictionary
- char** dictionaryData;
- int idx;
- //Main Function:
- int main(int argc, char **argv){
- //File variables
- FILE *f1, *f2, *f3;
- char *s;
- char buffer[1024], text[1024];
- //Open and read files
- f1 = fopen("dictionary.txt", "r");
- f2 = fopen("sentences_test.txt", "r");
- f3 = fopen("single_words_test.txt", "r");
- dictionaryData = malloc(45440 * sizeof(char*));
- int n = 0;
- while(fgets(buffer, 1024, f1) != NULL){
- //printf("%s\n", buffer);
- dictionaryData[n] = malloc(strlen(buffer)+1 * sizeof(char));
- //line[n] = text;
- strcpy(dictionaryData[n],buffer);
- n++;
- }
- /*printf("%d\n",n);
- printf("%s\n", dictionaryData[0]);
- printf("%s\n", dictionaryData[1]);
- printf("test");*/
- //Compare sentences_text file with dictionary file and print out the misspelt words to the new file
- while(fgets(text, 1024, f2) != NULL)
- {
- text[strlen(text)] = '\0';
- idx = search(text, dictionaryData, 45);
- //Create file
- FILE *file;
- //Open file
- file = fopen("misspeltwords.txt", "a");
- if(idx == 0){
- //Write the misspelt word to the file
- fprintf(file, "%s\n", text);
- }
- fclose(file);
- }
- //Compare single_words_test file with dictionary file and print out the misspelt words to the new file
- while(fgets(text, 1024, f3) != NULL);
- {
- text[strlen(text)]= '\0';
- idx = search(text, dictionaryData, 45);
- //printf("%d",idx);
- //Create file
- FILE *file;
- //Open file
- file = fopen("misspeltwords.txt","a");
- if(idx == 0){
- //Write the misspelt words to the file
- fprintf(file, "%s\n", text);
- }
- fclose(file);
- }
- /*int i;
- int dictionaryDataLength = strlen(dictionaryLen);
- for (i = 0; i <)*/
- fclose(f1);
- fclose(f2);
- fclose(f3);
- free(dictionaryData);
- return 1;
- }
- #include<string.h>
- #include<ctype.h>
- //Checking that the lowercase string from a file is same as a lowercase string from another file
- int strcmpIgnoreCase(char * one, char * two){
- int i;
- for(i = 0; i < strlen(one); ++i){
- one[i] = tolower(one[i]);
- }
- for(i = 0; i < strlen(two); ++i){
- two[i] = tolower(two[i]);
- }
- /*printf("Comparing words \n");
- printf("%s \n", one);
- printf("%s \n", two);
- */
- return strcmp(one, two);
- }
- char line[1024];
- //Binary serach the sorted array
- int search(char * s1, char ** s2, int dictSize){
- int first, last, middle;
- first = 0;
- last = dictSize - 1;
- while(first <= last)
- {
- middle = (first + last)/2;
- int res = strcmpIgnoreCase(s1, s2[middle]);
- if(res == 0) {
- printf("Correctly spelled");
- return middle;
- }
- if(res < 0) {
- first = middle + 1;
- }
- else{
- last = middle - 1;
- }
- middle = (first + last)/2;
- }
- return 0;
- }
- //put the dictionary in the the 2 dimensional array where each row would be one word in the dictionary
- char** dictionaryData;
- int idx;
- //Main Function:
- int main(int argc, char **argv){
- //File variables
- FILE *f1, *f2, *f3;
- char *s;
- char buffer[1024], text[1024];
- //Open and read files
- f1 = fopen("dictionary.txt", "r");
- f2 = fopen("sentences_test.txt", "r");
- f3 = fopen("single_words_test.txt", "r");
- dictionaryData = malloc(45440 * sizeof(char*));
- int n = 0;
- while(fgets(buffer, 1024, f1) != NULL){
- //printf("%s\n", buffer);
- dictionaryData[n] = malloc(strlen(buffer)+1 * sizeof(char));
- //line[n] = text;
- strcpy(dictionaryData[n],buffer);
- n++;
- }
- /*printf("%d\n",n);
- printf("%s\n", dictionaryData[0]);
- printf("%s\n", dictionaryData[1]);
- printf("test");*/
- //Compare sentences_text file with dictionary file and print out the misspelt words to the new file
- while(fgets(text, 1024, f2) != NULL)
- {
- text[strlen(text)] = '\0';
- idx = search(text, dictionaryData, 45);
- //Create file
- FILE *file;
- //Open file
- file = fopen("misspeltwords.txt", "a");
- if(idx == 0){
- //Write the misspelt word to the file
- fprintf(file, "%s\n", text);
- }
- fclose(file);
- }
- //Compare single_words_test file with dictionary file and print out the misspelt words to the new file
- while(fgets(text, 1024, f3) != NULL);
- {
- text[strlen(text)]= '\0';
- idx = search(text, dictionaryData, 45);
- //printf("%d",idx);
- //Create file
- FILE *file;
- //Open file
- file = fopen("misspeltwords.txt","a");
- if(idx == 0){
- //Write the misspelt words to the file
- fprintf(file, "%s\n", text);
- }
- fclose(file);
- }
- /*int i;
- int dictionaryDataLength = strlen(dictionaryLen);
- for (i = 0; i <)*/
- fclose(f1);
- fclose(f2);
- fclose(f3);
- free(dictionaryData);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement