Advertisement
Shanix

Untitled

Mar 15th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. /* The spellcheck.c component will contain the main() function and any other supporting functions you decide to use.
  2.  
  3. It will be responsible for handling the command-line arguments, finding words in the text, checking to see if they're in the dictionary, and responding to user commands.
  4.  
  5. Other than main(), you're not required to define any specific functions in spellcheck.c, but you must decide on at least two additional functions you'd like to use to simplify your implementation. You can break the problem down however you'd like, but you must use at least two non-trivial functions in addition to main. You can use more if you want.*/
  6.  
  7. #include <stdio.h>
  8. #include <stdbool.h>
  9. #include <ctype.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "text.h"
  13.  
  14. char **dictionary;
  15. int dictionarySize;
  16.  
  17. char **wordLines;
  18.  
  19. /**
  20. * Function to check if a word is in the dictionary. Can be
  21. * easily modded to take in a dictionary. Will loop through
  22. * the dictionary until it finds a word or until the given word
  23. * is smaller than current position - meaning it wasn't found.
  24. * @param word a char array to check if in dictionary.
  25. * @return 1 or 0 if found or not respectively.
  26. */
  27. int isInDictionary( char *word )
  28. {
  29. for ( int i = 0; i < dictionarySize; i++ ) {
  30. if ( strcmp( word, dictionary[i] ) == 0 ) {
  31. return 1;
  32. } else if ( strcmp( word, dictionary[i] ) < 0 ) {
  33. break;
  34. }
  35. }
  36. return 0;
  37. }
  38.  
  39. /**
  40. * Function to check spelling of a given line.
  41. * @param
  42. */
  43. void sentenceArray( char *line )
  44. {
  45. int capacity = 1;
  46. char *word = malloc( capacity );
  47. int index = 0;
  48. for ( int i = 0; i < strlen( line ); i++ ) {
  49. if ( !isalpha( line[i] ) ) {
  50. index = 0;
  51. //call the correction method
  52. for ( int j = 0; j < capacity; j++ ) {
  53. word[j] = '\0';
  54. }
  55. } else {
  56. word[index] = line[i];
  57. word[index + 1] = '\0';
  58. index++;
  59. if ( index == capacity ) {
  60. capacity *= 2;
  61. word = (char *)realloc(word, capacity);
  62. }
  63. }
  64. }
  65. }
  66.  
  67. /**
  68. * qsort comparing
  69. */
  70. int compareStrings( const void* a, const void* b )
  71. {
  72. const char *str1 = *(const char **)a;
  73. const char *str2 = *(const char **)b;
  74. int str1Len = strlen( str1 );
  75. int str2Len = strlen( str2 );
  76.  
  77. if ( str1Len >= str2Len ) {
  78. return strncmp(str1, str2, str1Len);
  79. } else {
  80. return strncmp(str1, str2, str2Len);
  81. }
  82.  
  83. }
  84.  
  85. void spellcheckLoop()
  86. {
  87. char input[5];
  88. }
  89.  
  90. /**
  91. * Program Start.
  92. */
  93. int main( int argc, char *argv[] )
  94. {
  95. // Check command-line arguments and open the input file.
  96. FILE *fp = fopen( argv[ 1 ], "r" );
  97. FILE *dictionaryFile = fopen( "words.txt", "r" );;
  98.  
  99. if ( fp == NULL ) {
  100. printf( "usage: spellcheck <textfile.txt> [words.txt]\n" );
  101. exit( EXIT_FAILURE );
  102. }
  103.  
  104. if ( argc == 3 ) {
  105. fclose( dictionaryFile );
  106. dictionaryFile = fopen( argv[ 2 ], "r" );
  107. }
  108.  
  109. fclose( fp );
  110. fclose( dictionaryFile );
  111.  
  112. if ( argc > 3 || argc < 2 ) {
  113. printf( "usage: spellcheck <textfile.txt> [words.txt]\n" );
  114. exit( EXIT_FAILURE );
  115. }
  116.  
  117. //Words from text files.
  118. int linesSize = 0;
  119. int *linePointer = &linesSize;
  120. wordLines = readLines( argv[ 1 ], linePointer );
  121.  
  122. //Dictionary.
  123. dictionarySize = 0;
  124. int *countPointer = &dictionarySize;
  125. if ( argc == 2 ) {
  126. dictionary = readLines( "words.txt", countPointer );
  127. } else {
  128. dictionary = readLines( argv[ 2 ], countPointer );
  129. }
  130.  
  131. qsort( dictionary, dictionarySize, sizeof( dictionary[0] ), compareStrings );
  132.  
  133. spellcheckLoop();
  134.  
  135. freeLines( dictionary, dictionarySize );
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement