home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STRTRIM.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  6KB  |  208 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /***************************************************************************
  4. * @(#)strtrim.c
  5. * @(#)strtrimr - Removes all trailing blanks from a string.
  6. * @(#)strtriml - Removes all leading blanks from a string.
  7. * @(#)strtrim  - Removes all leading and trailing blanks in a string.
  8. *
  9. ***************************************************************************
  10. *@(#)1993 Erik Bachmann
  11. *
  12. * Released to public domain 27-Oct-95
  13. ***************************************************************************/
  14.  
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include "bacstd.h"
  18.  
  19. /*
  20.  /-------------------------------------\
  21. |  STRTRIMR                             |------------------------------------|
  22. |\-------------------------------------/
  23. |
  24. | Removes all trailing blanks from a string.
  25. | Blanks are defined with ISSPACE  (blank, tab, newline, return, formfeed,
  26. | vertical tab = 0x09 - 0x0D + 0x20)
  27. |
  28. |----------------------------------------------------------------------------|
  29. | CALL:
  30. |    strtrimr(&str);
  31. |
  32. | HEADER:
  33. |    ctype.h
  34. |
  35. | GLOBALE VARIABLES:
  36. |    %
  37. |
  38. | ARGUMENTS:
  39. |    pszStr      : String to be converted
  40. |
  41. | PROTOTYPE:
  42. |    int _CfnTYPE strtrimr(char *pszStr);
  43. |
  44. | RETURN VALUE:
  45. |    j-i         : No of removed blanks
  46. |
  47. | MODULE:
  48. |    strtrim.c
  49. |----------------------------------------------------------------------------|
  50. |1994-01-08/Bac
  51. |   All characters is checked (">" -> ">=").
  52. |
  53. |
  54. |
  55. |----------------------------------------------------------------------------|
  56. |1992-11-09/Erik Bachmann
  57. \---------------------------------------------------------------------------|*/
  58. int _CfnTYPE strtrimr(char *pszStr)
  59. {
  60.       int   i, j;                               /* Local counters */
  61.  
  62.       /*-------------------------------------------------*/
  63.  
  64.       j = i = strlen(pszStr) - 1; /* Calculate the length of the string */
  65.  
  66.       while (isspace(pszStr[i]) && (i >= 0))
  67.  
  68.             /* WHILE string ends with a blank */
  69.             /*1994-01-08/Bac Even if all chars are blanks (= 0) */
  70.  
  71.             pszStr[ i-- ] = '\0';               /*- Replace blank with '\0' */
  72.  
  73.       return(j - i);                            /* Return no of replacements */
  74. }
  75.  
  76. /*
  77.  /-------------------------------------\
  78. |  STRTRIML                             |------------------------------------|
  79. |\-------------------------------------/
  80. |
  81. | Removes all leading blanks from a string.
  82. | Blanks are defined with ISSPACE  (blank, tab, newline, return, formfeed,
  83. | vertical tab = 0x09 - 0x0D + 0x20)
  84. |
  85. |
  86. |----------------------------------------------------------------------------|
  87. | CALL:
  88. |    strtriml(&str);
  89. |
  90. | HEADER:
  91. |    ctype.h
  92. |
  93. | GLOBALE VARIABLES:
  94. |    %
  95. |
  96. | ARGUMENTS:
  97. |    pszStr      : String to be converted
  98. |
  99. | PROTOTYPE:
  100. |    int _CfnTYPE strtriml(char *pszStr);
  101. |
  102. | RETURN VALUE:
  103. |    i           : No of removed blanks
  104. |
  105. | MODULE:
  106. |    strtrim.c
  107. |----------------------------------------------------------------------------|
  108. |
  109. |
  110. |----------------------------------------------------------------------------|
  111. |1992-11-09/Erik Bachmann
  112. \---------------------------------------------------------------------------|*/
  113.  
  114. int _CfnTYPE strtriml(char *pszStr)
  115. {
  116.       int   i = 0, j;                                 /* Local counters */
  117.  
  118.       /*-------------------------------------------------*/
  119.  
  120.       j = strlen(pszStr) - 1; /* Calculate the length of the string */
  121.  
  122.       while (isspace(pszStr[i]) && (i <= j))
  123.  
  124.             /* WHILE string starts with a blank */
  125.  
  126.             i++;                          /*- Count no of leading blanks */
  127.  
  128.       if (0 < i)                          /* IF leading blanks are found */
  129.             strcpy(pszStr, &pszStr[i]);   /*- Shift string to the left */
  130.  
  131.       return(i);                          /* Return no of replacements */
  132. }
  133.  
  134. /*
  135.  /-------------------------------------\
  136. |  STRTRIM                              |------------------------------------|
  137. |\-------------------------------------/
  138. | Removes all leading and trailing blanks in a string.
  139. | Blanks are defined with ISSPACE  (blank, tab, newline, return, formfeed,
  140. | vertical tab = 0x09 - 0x0D + 0x20)
  141. |
  142. |
  143. |----------------------------------------------------------------------------|
  144. | CALL:
  145. |    strtrim(&str);
  146. |
  147. | HEADER:
  148. |    ctype.h
  149. |
  150. | GLOBALE VARIABLES:
  151. |    %
  152. |
  153. | ARGUMENTS:
  154. |    pszStr      : String to be converted
  155. |
  156. | PROTOTYPE:
  157. |    int _CfnTYPE strtrim(char *pszStr);
  158. |
  159. | RETURN VALUE:
  160. |    iBlank      : No of removed blanks
  161. |
  162. | MODULE:
  163. |    strtrim.c
  164. |----------------------------------------------------------------------------|
  165. |
  166. |
  167. |
  168. |----------------------------------------------------------------------------|
  169. |1992-11-09/Erik Bachmann
  170. \---------------------------------------------------------------------------|*/
  171.  
  172. int _CfnTYPE strtrim(char *pszStr)
  173. {
  174.       int   iBlank;
  175.  
  176.       /*-------------------------------------------------*/
  177.  
  178.       iBlank  = strtrimr(pszStr);               /* Remove trailing blanks */
  179.       iBlank += strtriml(pszStr);               /* Remove leading blanks */
  180.  
  181.       return(iBlank);
  182. }
  183.  
  184.  
  185. #ifdef      TEST
  186.  
  187. main()
  188. {
  189.       char  *strl = "     Leading blanks",
  190.                   *strr = "Traling blanks     ",
  191.                   *str  = "   Lead-&trailing  ";
  192.       /*--------------------------------*/
  193.  
  194.       printf("\nBefore convertion:\n\t\"%s\"\n\t\"%s\"\n\t\"%s\"\n",
  195.                   strl, strr, str);
  196.  
  197.       strtrimr(strr);
  198.       strtriml(strl);
  199.       strtrim(str);
  200.  
  201.       printf("\nAfter convertion:\n\t\"%s\"\n\t\"%s\"\n\t\"%s\"\n",
  202.                   strl, strr, str);
  203.  
  204.       return(0);
  205. }
  206.  
  207. #endif
  208.