SO here is the code I need help with, basically I need to write a program that cheks if 5 words from a input document are in a dicionary document both .txt files I made, I really tried but cant find where to start, any help would be appriciated.

#include<stdio.h>
#define WORD_AMOUNT 5
#define LETTERS_PER_WORD 10
#define CASE_OFFSET 32
#define DICT "dictionary.txt"
#define INPUT "input.txt"

void test_populate_and_print();
void test_compares();
void test_find();
void test_swap();
void test_order_alphabetically();
void test_dictionary_find_and_order();

void populate_array_from_text_file
		(char array_strings[][LETTERS_PER_WORD], char file_name[]);
		
int is_word_in_dictionary(char word[], char file_name[]);

void print_all_words
	(char array_words[][LETTERS_PER_WORD], int word_amount);
	
void verify_all_input_file_words_in_dictionary
	(char input_words[][LETTERS_PER_WORD],
	char dictionary_file[], char input_file[], int word_amount);
		
void organize_words_alphabetically
		(char array_words[][LETTERS_PER_WORD]);	
		
void swap_words(char word1[], char word2[]);
	
int compare_strings_case_insensitive(char *str1, char *str2);

int compare_strings(char *str1, char *str2);

int compare_two_letters_case_insensitive(char letter1, char letter2);

void copy_from_first_to_second(char word1[], char word2[], int string_size);

void clean_up_words(char array_words[][LETTERS_PER_WORD], int word_amount);

void start_program();
	
int main()
{
	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("Verifying which words aren't in file:\n");
	verify_all_input_file_words_in_dictionary(input_words,DICT,INPUT, WORD_AMOUNT);
	organize_words_alphabetically(input_words);
	clean_up_words(input_words, WORD_AMOUNT);
	printf("Showing words found that are in file:\n");
	print_all_words(input_words, WORD_AMOUNT);
}

void test_dictionary_find_and_order()
{
	char input_words[WORD_AMOUNT][LETTERS_PER_WORD]={0};
	verify_all_input_file_words_in_dictionary(input_words,DICT,INPUT, WORD_AMOUNT);
	organize_words_alphabetically(input_words);
	clean_up_words(input_words, WORD_AMOUNT);
	print_all_words(input_words, WORD_AMOUNT);
}

void test_order_alphabetically()
{	
	char input_words[WORD_AMOUNT][LETTERS_PER_WORD]={0};
	populate_array_from_text_file(input_words,"input.txt");
	print_all_words(input_words, WORD_AMOUNT);
	printf("\n\n");
	organize_words_alphabetically(input_words);
	print_all_words(input_words, WORD_AMOUNT);
}

void test_swap()
{
	void swap_words(char word1[], char word2[]);
	void copy_from_first_to_second(char word1[], char word2[], int string_size);
	
	char str1[LETTERS_PER_WORD] = "str1";
	char str2[LETTERS_PER_WORD] = "str2";
	
	printf("str1:%s, str2:%s\n",str1,str2);
	swap_words(str1,str2);
	printf("str1:%s, str2:%s\n",str1,str2);
	
}

void test_populate_and_print()
{
	char input_words[WORD_AMOUNT][LETTERS_PER_WORD]={0};

	populate_array_from_text_file(input_words,"input.txt");
	print_all_words(input_words, WORD_AMOUNT);
	
	swap_words(input_words[0], input_words[4]);
	print_all_words(input_words, WORD_AMOUNT);
	
}

void test_compares()
{
	char str1[]="h";
	char str2[]="H";
	
	printf("Case insensitive comparison:\n%s vs. %s : %d\n"
		,str1,str2,compare_strings_case_insensitive(str1,str2));
	
	printf("Case sensitive comparison:\n%s vs. %s : %d\n"
		,str1,str2,compare_strings(str1,str2));
	
}

void test_find()
{
	int is_word_in_dictionary(char word[], char file_name[]);
	
	char word_to_find[] = "Digg";
	printf("Is word %s in dictionary:%d\n", 
			word_to_find,
			is_word_in_dictionary(word_to_find, "dictionary.txt"));
	
}

//	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)
{	

}

//	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[])
{	

}

//	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)
{

}

//	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)
{

}

//	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[])
{

}

//	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[])
{

}

//	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])
{	

}

//	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 string_size)
{

}

//	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[])
{

}

//	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)
{

}

//	Function that moves down in the array the position of a certain key value, 
//	like a null word.
//
//	Parameters:
//	array_words = the array of words.
//	word_amount = the amount of words in the array.
//
//	Self-defined functions used:
//	swap_words
void clean_up_words(char array_words[][LETTERS_PER_WORD], int word_amount)
{

}

Recommended Answers

All 6 Replies

so you've posted your assignment.

what is your question?

Well for starters I would like to know how to proceed to do this function:

//	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)
{
 
}

This is just one of the 10 function I need to make, any other help regarding the other functions will be appriciated.

what does "No self-defined functions used" mean? that doesn't make any sense, so it seems to me you could just do this:

int compare_strings( char *str1, char *str2)
{
    return strcmp(str1, str2);
}

but that would be retarded to put a wrapper around a library function, and since this is (obviously) your homework assignment, i'm sure that's not what your instructor is looking for.

so you need to look at each character in each string and compare them against the other. if they all match, return zero. if they dont match, how do they not match? if the letter in str1 comes before str2 (alphabetically) then that letter in str1 will have a lower ascii value than the letter in str2

now, how are the letter stored in the strings in the first place? here's an example:

str1 = "Hello World"
str2 = "Hello Werds"

*str1 = 'H' = 0x48 (dec 72)
*str2 = 'H' = 0x48 (dec 72)

(*str1 +1) = 'e' = 0x65 (dec 101)
(*str2 +1) = 'e' = 0x65 (dec 101)

(*str1 + 5) = ' ' = 0x20 (dec 32)
(*str2 + 5) = ' ' = 0x20 (dec 32)

(*str1 + 7) = 'o' = 0x6F (dec 111)
(*str2 + 7) = 'e' = 0x65 (dec 101)

of course you dont cycle through each element using +1, +2, +3, etc. you will use the ++ increment operator to increment the pointers by one each time through a conditional loop such as 'while' or 'for'.

and how do you compare each letter? One way is simple subtraction.

int result;

result = *str1 - *str2;
if (result == 0)
{
   str1++; 
   str2++;  //increment pointers
}

else 
   return result; // value will be negative if *str1<*str2
                  // value will be positive if *str1>*str2

now, incorporate this in a loop that continues while each element of string is not a null character (0). if one string ends before the other, then you also have an inequality that must be handled.

some might think i'm borderline on having given you too much code without effort on your part. Perhaps, but I'm going to give you the benefit of the doubt. i believe you will complete this function, verify that it works, and start the next one on your own. be sure post what you've done here.


.

commented: The last C syntax is nice +0

I see so could this be a solution

int compare_strings(char str1, char str2)
{   
    int counter = 0;
    for(counter = 0; *str1!= '\0' && *str2 != '\0'; 
    counter++)
    {          int result;
 
              result = *str1 - *str2;
              if (result == 0)
    {
                         str1++; 
                         str2++;  
    }
 
else 
   return result; 
    }

If I subtract both strings would that just give me which word is longer? and I need them in alphabetical order.

I see so could this be a solution

you're using that "for" loop to do the function of a "while" loop. your 'counter' variable is meaningless. if you want a while loop, then just use a while loop.

another problem is that the two pointers are often not going to be pointing to '\0' (NULL character) at the same time. the memory locations after the terminating NULL are not guaranteed to be NULL. you could wind up with a big mess.

what happens when one string is longer than the other? one will be Null and one will not. why would you want to keep looping in that case? get out and determine which string ended. that string will be alphabetically "before" the other. e.g. "internets" comes after "internet"

If I subtract both strings would that just give me which word is longer? and I need them in alphabetical order.

no. you would get the difference between the two addresses, which would be meaningless.

"strlen" will give you the length of a string. since you're having to write your own "strcmp" function, you may not be allowed to use the <string.h> library. doesnt matter, though, because if you just use the while loop and jump out when the first string hits the end (the NULL character), you don't need to know the string lenght.

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.