Advertisement
Guest User

Untitled

a guest
Jan 9th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1. /*
  2.  * File: verify_word.c
  3.  * Author: Miles Tjandrawidjaja
  4.  * Last Updated: 11/25/2012
  5.  * =========================
  6.  * Description: This will take in a dictionary text file, and input text file, and verify input
  7.  *              text files for valid words. Ouput file is words.txt.Dictionary file is dictionary.txt            
  8.  *
  9.  */
  10.  
  11.  
  12. /*
  13.  * Notes
  14.  * ==========
  15.  * 1. If comparing strings (*char) with ==
  16.  *    You are actually comparing their addresses
  17.  *    Solution to this is use strcmp or strcasecmp
  18.  */
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. typedef int bool;
  25. #define True 1
  26. #define False 0
  27.  
  28. #define MAXSIZE 6
  29. #define MAXWORDS 50
  30. /* Function Prototypes */
  31. void CheckFile(char *input_file, char *dictionary_file, char *output_file, char **valid_words);
  32. char *trimwhitespace(char *str);
  33.  
  34. int main(void){
  35.     char *input_name = "permutations.txt";
  36.     char *dictionary_name = "dictionary.txt";
  37.     char *output_name = "words.txt";
  38.     char *valid_words[MAXWORDS];
  39.     CheckFile(input_name, dictionary_name, output_name, valid_words);
  40.     printf("Pass\n");
  41.     int temp = 0;
  42.     for (temp=0; temp<MAXWORDS; temp++){
  43.         printf("%s\n",valid_words[temp]);
  44.     }  
  45.     return 0;
  46. }
  47.  
  48. /*
  49.  * Function: CheckFile
  50.  * =============================
  51.  * Descriptions: Takes in list of words and dictionary files, and appends to specified
  52.  *               output file
  53.  *              
  54.  */
  55. void CheckFile(char *input_file, char *dictionary_file, char *output_file, char **valid_words)
  56. {
  57.     /* Open files as read or append */
  58.     FILE *input, *dictionary, *output;
  59.     input = fopen(input_file, "r");
  60.     dictionary = fopen(dictionary_file, "r");
  61.     output = fopen(output_file,"a");
  62.     int word_count = 0;
  63.  
  64.     /* Read from files */
  65.     if (input != NULL && dictionary!= NULL)
  66.     {  
  67.         char *iline = malloc(MAXSIZE*sizeof(char));
  68.         char *dline = malloc(MAXSIZE*sizeof(char));
  69.         while (fgets (iline, sizeof iline, input) != NULL)
  70.         {
  71.             while (fgets (dline, sizeof dline, dictionary) != NULL)
  72.             {
  73.                 trimwhitespace(iline);
  74.                 trimwhitespace(dline);
  75.                 if (strcasecmp(iline, dline) == 0 )
  76.                 {
  77.                     fprintf(output, "%s\n",iline);
  78.                     valid_words[word_count] = iline;
  79.                     printf("Word Count: %d\n", word_count);
  80.                     printf("At Zero: %s\n", valid_words[0]);
  81.                     printf("Valid Word: %s\n", valid_words[word_count]);
  82.                     printf("Actual Word: %s\n", iline);
  83.                     word_count++;
  84.                 }
  85.  
  86.             }
  87.             rewind(dictionary);
  88.         }
  89.  
  90.         fclose(input);
  91.         fclose(dictionary);
  92.         fclose(output);
  93.         free(iline);
  94.         free(dline);
  95.     }
  96.     else
  97.     {
  98.         printf("An error has occured\n");
  99.     }
  100. }
  101.  
  102. /*
  103.  * Function: trimwhitesapce(char *str)
  104.  * Description: This function removes any whitespace
  105.  */
  106. char *trimwhitespace(char *str)
  107. {
  108.   char *end;
  109.  
  110.   /* Trim leading Whitespace */
  111.   while(isspace(*str)) str++;
  112.  
  113.   /* Check if string is empty*/
  114.   if(*str == 0) { return str; }
  115.  
  116.   /* Trim trailing whitesapce */
  117.   end = str + strlen(str) - 1;
  118.   while(end > str && isspace(*end)) end--;
  119.  
  120.   /* Add new terminating character */
  121.   *(end+1) = 0;
  122.  
  123.   return str;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement