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);
	
}
Salem commented: No code tags === loss of readers -4
Dave Sinkula commented: Yup. I lost interest. -2

Recommended Answers

All 6 Replies

456 lines of code - That is the best you can do. You can't narrow the problem to a certain section of this program?

456 lines of code - That is the best you can do. You can't narrow the problem to a certain section of this program?

I would if I knew which part was causing the problem, there is no problem with the compiler there might be something missing that I do not know. Any help with the project would be appriciated if you find any other alternative to the objective of this program, or another code, that would solve the purpose.

As an overview, the project should do the following.

1) Verify the words given in the input.txt file that are in the dictionary.txt file.
This should be case insensitive. In other words, if in input.txt you have the word "Hello",
and in dictionary.txt you have the words "hello", this counts as having found the word.
The words not found in dictionary.txt, should be printed out with the message:
"The word %s was not found in the dictionary" where %s is to be substituted by the word not found.
2) The words from input.txt that were found in the dictionary.txt file, on the other hand, should be inserted into an array.
3) The array of words found in the dictionary, should then be ordered alphabetically, and then printed on screen with the header: "The words that were found are:" preceded by the order in which they were found.
For example:
The words that were found are:
3) give
2) gonna
1) never
4) up

4) Capital letters have precedence over lower case ones when sorting.
For example, if the words to order were: "Hello, alf, big, Zoey", then the order would be:
1) Hello
2) Zoey
3) alf
4) big

This is due friday so Im in a bit of a hurry.

So start by doing some tests (or did you hope that you could write 500 lines and it would work first time).

Eg.

int main ( ) {
  printf( "Number of words in file=%d\n", words_in_file("test.txt") );
  return 0;
}

Keep testing that ONE function until it returns a good result for all your tests,

Then move on to the next function (which doesn't depend on other functions), say your comparisons.

Build up the picture of what works and what doesn't.

commented: Helpful suggestion. +6

I have done some debugging and got an "acces vilation raised program" error, anyone know what that means, Im using dev c++ 4.9.9.2 , any suggestions?

Your program tried to access outside the bounds of your variables -- for example trying to access var[20] when the definition is int var[10];

I tried looking at the code and got to the function

void populate_array_from_text_file(char array_strings[][LETTERS_PER_WORD], char file_name[])

where the program opens a file without checking to see if it succeeded and then reads from that file without checking for EOF.....

Any ways here's a cleaner posting of the code...by the way, why do you this is some parts of your code - return & break

else if(str1[result]=='\0' && str2[result] == '\0')
		{	
			return 0;
			break;
		}

Plus this little odd ball

if    (compare_two_letters_case_insensitive(str1[i],str2[i]) == 1)           
		{
			;
		}

cleaner posting

#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; 
	
int main(void)
{
	start_program() ;
	return 0;
}


void start_program()
{
	char input_words[WORD_AMOUNT][LETTERS_PER_WORD]={{0},{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") ;
}


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++)                                                   
	{	
		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)    
		{	
			copy_from_first_to_second(array_from_input[i],input_words[words_in_new_array++]);	
		}
	}
}

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)           
		{
			;
		} 
		else if(compare_two_letters_case_insensitive(str1[i],str2[i])== 0)       
		{ 
			return 0 ;
			break ;
		}
		else if((str1[i] == '\0') && (str2[i] == '\0'))                           
		{ 
			return 1 ;
			break;
		}
		else if(((str1[i]== '\0')&& (str2[i] != '\0')) || ((str2[i] == '\0')&& (str1[i] != '\0'))) 
		{ 
			return 0;
			break;
		}
		else
		{
			system("exit") ; 
		}

	}
}


int compare_strings(char str1[], char str2[])
{
	int result;                             

	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;
		}		
	}	

}


int compare_two_letters_case_insensitive(char letter1, char letter2)
{
	if((letter1< 91) && (letter1 > 64))                          
	{
		if((letter2- letter1 ==CASE_OFFSET)|| (letter2 - letter1 == 0))
		{
			return 1;                                                  
		}
		else
			return 0; 
	}
	else

	if((letter1 < 123) && (letter1 > 96))                          
	{	
		if((letter1 - letter2== CASE_OFFSET) || (letter1 - letter2 == 0))
		{
			return 1;                                          
		}
		else
		{
			return 0; 
		}
	}
	else

	return -1; 
}


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;                                         

	current_char = fgetc(inputFile);                              


	for(i= 0; current_char != '@'; i++)
	{
		for (j = 0;((current_char != ',') && (current_char != '@')); j++)
		{			
			fputs("got to here!\n", stdout);/**********************/		
			array_strings[i][j] = current_char;
			current_char = fgetc(inputFile);                 
		}
		if (current_char== ',')
		{
			array_strings[i][j] = '\0';
			current_char= fgetc(inputFile); 
		}
		else if (current_char == '@')
		{
			array_strings[i][j] = '\0';                                
		}
	}
}

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++)
	{
		if(compare_strings_case_insensitive(word,dictionary_array[i])== 1)
		{	
			return 1; 
			break;
		}
		else if(i== WORDS_IN_DICT)
		{
			return 0; 
      			break;
		}
	}
}


void organize_words_alphabetically(char array_words[][LETTERS_PER_WORD],int words_in_array)
{	
	int i;                                                            
	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]);
			}
		}
	}
}


void copy_from_first_to_second(char word1[], char word2[])
{
	int i = 0; 

	for (i = 0; ;i++)                              
	{
		word2[i] = word1[i];
		if (word1 [i] == '\0') 							
			break ;
	}
}


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) ;
}

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") ;
}

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);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.