Advertisement
Shanix

Untitled

Mar 15th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. char **dictionary;
  2. int dictionarySize;
  3.  
  4. char **wordLines;
  5. int linesSize;
  6.  
  7. /**
  8. * qsort comparing
  9. */
  10. int compareStrings( const void* a, const void* b )
  11. {
  12. const char *str1 = *(const char **)a;
  13. const char *str2 = *(const char **)b;
  14. int str1Len = strlen( str1 );
  15. int str2Len = strlen( str2 );
  16.  
  17. if ( str1Len >= str2Len ) {
  18. return strncmp(str1, str2, str1Len);
  19. } else {
  20. return strncmp(str1, str2, str2Len);
  21. }
  22.  
  23. }
  24.  
  25. /**
  26. * Function to check if a word is in the dictionary. Can be
  27. * easily modded to take in a dictionary. Will loop through
  28. * the dictionary until it finds a word or until the given word
  29. * is smaller than current position - meaning it wasn't found.
  30. * @param word a char array to check if in dictionary.
  31. * @return 1 or 0 if found or not respectively.
  32. */
  33. int isInDictionary( char *word )
  34. {
  35. //word to be compared, must be made lowercase
  36. //but we don't want to modify the word itself.
  37. char lowercaseWord[ strlen( word ) + 1 ];
  38. for ( int i = 0; i < strlen( word ); i++ ) {
  39. lowercaseWord[i] = tolower( word[i] );
  40. }
  41.  
  42. lowercaseWord[ strlen( word ) ] = '\0';
  43. for ( int i = 0; i < dictionarySize; i++ ) {
  44. if ( strcmp( lowercaseWord, dictionary[i] ) == 0 ) {
  45. return 1;
  46. } else if ( strcmp( lowercaseWord, dictionary[i] ) < 0 ) {
  47. break;
  48. }
  49. }
  50.  
  51. // int *value = bsearch( lowercaseWord, dictionary, dictionarySize, sizeof( dictionary[0] ), compareStrings );
  52.  
  53. return 0;
  54. }
  55.  
  56. /**
  57. * When called, markMisspelled will change color of output
  58. * to red. Once called again, standard color.
  59. */
  60. void markMisspelled( char *word )
  61. {
  62. printf( "\x1B[31m" );
  63. printf( "%s", word );
  64. printf( "\x1B[0m" );
  65. }
  66.  
  67. /**
  68. * Function to check spelling of a given line.
  69. * @param
  70. */
  71. int checkLineForMisspelling( char *line )
  72. {
  73. char **wordsInLine = malloc( 100 * sizeof(char *) );
  74.  
  75. char *token = strtok (line, " ");
  76. int index = 0;
  77. while (token != NULL) {
  78. wordsInLine[index] = token;
  79. index++;
  80. token = strtok (NULL, " ");
  81. }
  82.  
  83. //read through line, if strings match, compare, else just print.
  84.  
  85. for (int i = 0; i < index; i++) {
  86. if ( isInDictionary( wordsInLine[i] ) ) {
  87. printf( "%s", wordsInLine[i] );
  88. } else {
  89. markMisspelled( wordsInLine[i] );
  90. }
  91. // printf( "%s\n", wordsInLine[i] );
  92. }
  93.  
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement