Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: verify_word.c
- * Author: Miles Tjandrawidjaja
- * Last Updated: 11/25/2012
- * =========================
- * Description: This will take in a dictionary text file, and input text file, and verify input
- * text files for valid words. Ouput file is words.txt.Dictionary file is dictionary.txt
- *
- */
- /*
- * Notes
- * ==========
- * 1. If comparing strings (*char) with ==
- * You are actually comparing their addresses
- * Solution to this is use strcmp or strcasecmp
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- typedef int bool;
- #define True 1
- #define False 0
- #define MAXSIZE 6
- #define MAXWORDS 50
- /* Function Prototypes */
- void CheckFile(char *input_file, char *dictionary_file, char *output_file, char **valid_words);
- char *trimwhitespace(char *str);
- int main(void){
- char *input_name = "permutations.txt";
- char *dictionary_name = "dictionary.txt";
- char *output_name = "words.txt";
- char *valid_words[MAXWORDS];
- CheckFile(input_name, dictionary_name, output_name, valid_words);
- printf("Pass\n");
- int temp = 0;
- for (temp=0; temp<MAXWORDS; temp++){
- printf("%s\n",valid_words[temp]);
- }
- return 0;
- }
- /*
- * Function: CheckFile
- * =============================
- * Descriptions: Takes in list of words and dictionary files, and appends to specified
- * output file
- *
- */
- void CheckFile(char *input_file, char *dictionary_file, char *output_file, char **valid_words)
- {
- /* Open files as read or append */
- FILE *input, *dictionary, *output;
- input = fopen(input_file, "r");
- dictionary = fopen(dictionary_file, "r");
- output = fopen(output_file,"a");
- int word_count = 0;
- /* Read from files */
- if (input != NULL && dictionary!= NULL)
- {
- char *iline = malloc(MAXSIZE*sizeof(char));
- char *dline = malloc(MAXSIZE*sizeof(char));
- while (fgets (iline, sizeof iline, input) != NULL)
- {
- while (fgets (dline, sizeof dline, dictionary) != NULL)
- {
- trimwhitespace(iline);
- trimwhitespace(dline);
- if (strcasecmp(iline, dline) == 0 )
- {
- fprintf(output, "%s\n",iline);
- valid_words[word_count] = iline;
- printf("Word Count: %d\n", word_count);
- printf("At Zero: %s\n", valid_words[0]);
- printf("Valid Word: %s\n", valid_words[word_count]);
- printf("Actual Word: %s\n", iline);
- word_count++;
- }
- }
- rewind(dictionary);
- }
- fclose(input);
- fclose(dictionary);
- fclose(output);
- free(iline);
- free(dline);
- }
- else
- {
- printf("An error has occured\n");
- }
- }
- /*
- * Function: trimwhitesapce(char *str)
- * Description: This function removes any whitespace
- */
- char *trimwhitespace(char *str)
- {
- char *end;
- /* Trim leading Whitespace */
- while(isspace(*str)) str++;
- /* Check if string is empty*/
- if(*str == 0) { return str; }
- /* Trim trailing whitesapce */
- end = str + strlen(str) - 1;
- while(end > str && isspace(*end)) end--;
- /* Add new terminating character */
- *(end+1) = 0;
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement