So I have this C code which is supposed to check if words from a input.txt are in a dictionary.txt document that I have created. Words in both texts are separeted by comas ej. dog,house,car@ and end with the "@" char. So far I have adjusted the code and it compiles but it does not run properly ie it starts but then is shuts down. I would like to know if there is something missing or wrong with the code. Any help would be appriciated thx.
Here is the code:
#define WORDS_IN_DICT words_in_file(DICT)
#include<stdio.h>
#define WORD_AMOUNT 5
#define LETTERS_PER_WORD 10
#define CASE_OFFSET 32
#define DICT "dictionary.txt"
#define INPUT "input.txt"
#include <stdlib.h>
void swap_words(char word1[], char word2[]);
void copy_from_first_to_second(char word1[], char word2[]);
void organize_words_alphabetically(char array_words[][LETTERS_PER_WORD],int words_in_array);
int compare_two_letters_case_insensitive(char letter1, char letter2);
void populate_array_from_text_file(char array_strings[][LETTERS_PER_WORD], char file_name[]);
int compare_strings_case_insensitive(char str1[], char str2[]);
void print_all_words(char array_words[][LETTERS_PER_WORD], int word_amount);
int words_in_file(char file_name[]);
int compare_strings(char str1[], char str2[]);
void verify_all_input_file_words_in_dictionary (char input_words[][LETTERS_PER_WORD],char dictionary_file[], char input_file[], int word_amount);
int is_word_in_dictionary(char word[], char file_name[]);
void start_program() ;
int words_in_new_array = 0; // number of words which are in array and Dic, puts the number in new array
int main(void)
{
start_program() ;
return 0;
}
// Function that will be called that has the logic of the program.
//
// Self-defined functions used:
// verify_all_input_file_words_in_dictionary
// organize_words_alphabetically
// clean_up_words
// print_all_words
void start_program()
{
char input_words[WORD_AMOUNT][LETTERS_PER_WORD]={0};
printf("\nVerifying which words aren't in file:\n\n") ;
verify_all_input_file_words_in_dictionary(input_words,DICT,INPUT, WORD_AMOUNT);
organize_words_alphabetically(input_words,words_in_new_array) ;
printf("\nShowing words found that are in file:\n");
print_all_words(input_words, words_in_new_array);
printf ("\n") ;
system ("pause") ;
}
// Verifies the words in the file specified by "input_file" are in
// the file "dictionary_file" and prints the words that aren't in it.
// It also populates the array input_words with the words from the
// "input_file" file that ARE in the "dictionary_file".
//
// Parameters:
// input_words = an array of strings.
// dictionary_file = the file name of the dictionary file to read.
// input_file = the file name of the input file to read.
// word_amount = amount of words in array.
//
// Self-defined functions used:
// populate_array_from_text_file
// is_word_in_dictionary
void verify_all_input_file_words_in_dictionary
(char input_words[][LETTERS_PER_WORD],
char dictionary_file[], char input_file[], int word_amount)
{
int i;
char array_from_input[WORD_AMOUNT][LETTERS_PER_WORD];
populate_array_from_text_file(array_from_input,input_file);
for(i = 0; i < word_amount; i++) // print words that are not in dict.
{
if (is_word_in_dictionary(array_from_input[i], dictionary_file) == 0)
{
printf("-The word \"%s\" was not found in the dictionary\n",array_from_input[i]);}
else
if (is_word_in_dictionary(array_from_input[i],dictionary_file) == 1) // copys words in dict to the new array
{
copy_from_first_to_second(array_from_input[i],input_words[words_in_new_array++]);
}
}
}
// Function will be used to determined whether two strings are the same
// being case insensitive.
// return 0, when there is NO difference between the two letters case insensitive.
// return 1, when there is a difference between the two letters case insensitive.
//
// Parameters:
// str1 = first string to compare.
// str2 = second string to compare.
//
// Self-defined functions used:
// compare_two_letters_case_insensitive
int compare_strings_case_insensitive (char str1[], char str2[])
{
int i ;
for (i = 0; ;i++)
{
if (compare_two_letters_case_insensitive(str1[i],str2[i]) == 1) // Loop does not have to check every condition on each pass
{;}
else
if(compare_two_letters_case_insensitive(str1[i],str2[i])== 0) // if both characters are different on the same place
{
return 0 ;
break ;
}
else
if((str1[i] == '\0') && (str2[i] == '\0')) // if both strings have the same characters
{
return 1 ;
break;
}
else
if((str1[i]== '\0')&& (str2[i] != '\0') || (str2[i] == '\0')&& (str1[i] != '\0')) // if one string is less than the other
{
return 0;
break;
}
else
{
system("exit") ;
}
}
}
// Function will compare two strings while being case sensitive.
// return 0, when there is NO difference between the two letters case sensitive.
// return positive when str1 is first in alphabetical order against str2.
// return negative when str1 is second in alphabetical order against str2.
//
// Parameters:
// str1 = first string to compare.
// str2 = second string to compare.
//
// NO self-defined functions used
int compare_strings(char str1[], char str2[])
{
int result; // This function subtracts both strings and returns either + or -, else 0
for (result = 0; ;result++)
{
if (str1[result]!= str2[result])
{
return str2[result] - str1[result];
break;
}
else
if(str1[result]=='\0' && str2[result] == '\0')
{
return 0;
break;
} }
}
// Compares two letters to verify that they are the same letter, regardless
// of case.
// return 0, when there is NO difference between the two letters case insensitive.
// return 1, when there is a difference between the two letters case insensitive.
//
// Parameters:
// letter1 = first character to compare.
// letter2 = second character to compare.
//
// NO self-defined functions used
int compare_two_letters_case_insensitive(char letter1, char letter2)
{
if
((letter1< 91) && (letter1 > 64)) //letter1 is upper-case
{
if
((letter2- letter1 ==CASE_OFFSET)|| (letter2 - letter1 == 0))
{
return 1; // returns same char
}
else
return 0;
}
else
if
((letter1 < 123) && (letter1 > 96)) //letter1 is lower-case
{ if
((letter1 - letter2== CASE_OFFSET) || (letter1 - letter2 == 0))
{
return 1; // is the same char
}
else
{
return 0;
}
}
else
return -1; // If either of the chars are not letters
}
// Populate the array "array_strings" with the words in "file_name" file.
//
// Parameters:
// array_strings = array in which all words will be stored.
// file_name = file name of file that contains the words to be stored.
//
// NO self-defined functions used
void populate_array_from_text_file
(char array_strings[][LETTERS_PER_WORD], char file_name[])
{
int i;
int j;
FILE *inputFile =fopen(file_name,"r");
char current_char; // Holds the character
current_char = fgetc(inputFile); // Places the first character in file
for(i= 0;current_char != '@'; i++)
{
for
(j = 0;(current_char != ',' && current_char != '@'); j++)
{
array_strings[i][j] = current_char;
current_char = fgetc(inputFile); // Used to move through strings in file
}
if
(current_char== ',')
{
array_strings[i][j] = '\0';
current_char= fgetc(inputFile);
}
else
if
(current_char == '@')
{
array_strings[i][j] = '\0'; // If char is null, stop .
}
}
}
// Function that verifies if "word" word is in the file named
// "file_name"
// return 1 when the word is in the dictionary
// return 0 when the word is not in the dictionary
//
// Parameters:
// word = the word to verify if it is in the file.
// file_name = the name of the file in which to check the word.
//
// Self-defined functions used:
// compare_strings_case_insensitive
int is_word_in_dictionary(char word[],char file_name[])
{
int i ;
char dictionary_array[20][LETTERS_PER_WORD];
populate_array_from_text_file(dictionary_array,DICT) ;
for
(i = 0; ;i++)
{
// compares each string to see if the word is found in dic, 1 for yes and 0 for not.
if
(compare_strings_case_insensitive(word,dictionary_array[i])== 1)
{
return 1;
break;
}
else
if(i== WORDS_IN_DICT)
{
return 0;
break ;
}
}
}
// Function that organizes alphabetically the words.
// Other considerations for the alphabetical order are:
// *Words with capital letters are first.
//
// Parameters:
// array_words = the array of words to put in alphabetical order.
//
// Self-defined functions used:
// compare_strings
// swap_words
void organize_words_alphabetically(char array_words[][LETTERS_PER_WORD],int words_in_array)
{
int i; //Quick-sort
int j ;
for
(i= 0; i < words_in_array;i++)
{
for(j = 0; j < words_in_array; j++)
{ if
(compare_strings(array_words[i],array_words[j]) > 0)
{swap_words(array_words[i],array_words[j]); }
}
}
}
// Copies the first string to the second string
//
// Parameters:
// word1 = the string to copy to another variable.
// word2 = will be copied to the word1 string.
// string_size = the size of the string to copy.
// This size should not be larger than the size of word2 nor word1.
//
// NO self-defined functions used
void copy_from_first_to_second(char word1[], char word2[])
{
int i= 0;
for
(i = 0; ;i++) //This functions copys each char one by one
{
word2[i] = word1[i];
if (word1 [i] == '\0')
break ;
}
}
// Function that swaps the two strings given.
// Parameters:
// word1 = first string
// word2 = second string
//
// Self-defined functions used:
// copy_from_first_to_second
void swap_words(char word1[], char word2[])
{
char temp[LETTERS_PER_WORD] ;
copy_from_first_to_second(word1,temp) ;
copy_from_first_to_second(word2,word1) ;
copy_from_first_to_second(temp,word2) ;
}
// Function that prints all the words in the array.
//
// Parameters:
// array_words = the array of words to print out.
// word_amount = the amount of words in the array.
//
// NO self-defined functions used
void print_all_words(char array_words[][LETTERS_PER_WORD], int word_amount)
{
int i ;
for
(i = 0; i != word_amount; i++)
{
printf("\n%d) %s",i + 1,array_words[i]) ;
}
printf("\n") ;
}
// This function safes the char that is being read and the first char in file, then counts the words
int words_in_file(char file_name[])
{
FILE *inputFile = fopen (file_name,"r") ;
char current_char ;
int i = 0;
current_char = fgetc(inputFile);
while
(current_char != '@')
{
if
(current_char == ',')
{i++;}
current_char = fgetc(inputFile);
}
return (i + 1);
}