Guest User

Untitled

a guest
Feb 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. Abbreviate a given string using a dictionary of acceptable abbreviations.
  2.  
  3.  
  4. Notes:
  5. Only "whole words" should be abbreviated. That is, if the word "record" can be abbreviated to "rec", "records" SHOULD NOT be abbreviated to "recs".
  6.  
  7. * A "whole word" is a sequence of alphanumeric characters.
  8. * If the abbreviated string will not fit in the destination buffer clamp the string.
  9. * Bonus points for allowing the substitution to be case insensitive, but also case aware. It should handle three cases, the original word all lower case, all upper case, or the first letter only being capitalized. Mixed cases can be handled in any reasonable way.
  10. * For example, if the dictionary contains: "record"->"rec":
  11. - "record" should be replaced with "rec"
  12. - "Record" should be replaced with "Rec"
  13. - "RECORD" should be replaced with "REC"
  14. - "ReCord" can be replaced with "rec", "Rec" or "REC"
  15.  
  16. * @param str source string to abbreviate (NULL terminated)
  17. * @param str_abbr destination buffer to store abbreviated version of 'str'
  18. * @param str_abbr_len length of 'str_abbr' buffer, including NULL terminator
  19. * @param dictionary array of acceptable abbreviations
  20. * @param dictionary_len number of items in dictionary
  21.  
  22. - File named abbr.c
  23.  
  24. #include "abbr.h"
  25.  
  26. void abbreviate_string(const char * str, char * str_abbr, size_t str_abbr_len, dictionary_t * dictionary, size_t dictionary_len)
  27. {
  28. const char* inputString = str;
  29. char ** wordArray = NULL;
  30. int numOfWords = 0;
  31.  
  32. char* word = strtok (inputString," ,-.:;");
  33. while (word != NULL)
  34. {
  35. wordArray = realloc (wordArray, sizeof (char*) * ++numOfWords);
  36.  
  37. //Check memory allocation
  38. if(wordArray == NULL)
  39. exit(-1);
  40.  
  41. wordArray[numOfWords - 1] = word;
  42. word = strtok (NULL, " ,-.:;");
  43. }
  44.  
  45. memset(str_abbr, 0, str_abbr_len - 1);
  46. int i;
  47. int stringLength = 0;
  48. for(i=0; i < numOfWords; ++i )
  49. {
  50. const char* result = CheckMatchinDictionary(wordArray[i], dictionary, dictionary_len );
  51.  
  52. // Append to the str_abbr only if fits in the destination buffer
  53. if( stringLength < str_abbr_len - 1)
  54. {
  55. if(result != NULL)
  56. {
  57. strcat(str_abbr, result);
  58. stringLength += strlen(result);
  59. }
  60. else
  61. {
  62. strcat(str_abbr, wordArray[i]);
  63. stringLength += strlen(wordArray[i]);
  64. }
  65.  
  66. strcat(str_abbr, " ");
  67. stringLength += 1;
  68. }
  69. else
  70. {
  71. if(result != NULL)
  72. strncat(str_abbr, result, str_abbr_len - stringLength - 1);
  73. else
  74. strncat(str_abbr, wordArray[i], str_abbr_len - stringLength - 1 );
  75. }
  76.  
  77. }
  78. //printf("The abbreviated string is %sn", str_abbr);
  79.  
  80. }
  81.  
  82. else if(strcasecmp(dictionary[index].full, input) == 0)
  83. {
  84. size_t length = strlen(dictionary[index].abbr);
  85. char* substr = malloc(sizeof(char) * length);
  86. strncpy(substr, input, length);
  87. substr[length + 1] = '';
  88. return substr;
  89. }
  90.  
  91. }
  92.  
  93. return NULL;
  94. }
  95.  
  96. //main function
  97.  
  98. #include <string.h>
  99. #include <stdio.h>
  100. #include "abbr.h"
  101.  
  102. typedef struct
  103. {
  104. const char * full;
  105. const char * abbr;
  106. } dictionary_t;
  107.  
  108. int main(void)
  109. {
  110. /* Sample Dictionary */
  111. dictionary_t dictionary[] =
  112. {
  113. {"record", "rec"},
  114. {"maximum", "max"},
  115. {"minimum", "min"},
  116. {"number", "no"}
  117. };
  118.  
  119. char str[100];
  120. char str_abbr[100];
  121.  
  122. strcpy(str, "maximum record rate is:");
  123.  
  124. abbreviate_string(str, str_abbr, sizeof(str_abbr), dictionary, sizeof(dictionary) / sizeof(dictionary[0]));
  125.  
  126. printf("%s -> %sn", str, str_abbr);
  127.  
  128. return 0;
  129. }
Add Comment
Please, Sign In to add comment