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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /***************************************************************************
  4. * @(#)strtrimc.c
  5. * @(#)strtrimcr
  6. * @(#)      Removes all trailing occurences of specific characters in a string.
  7. * @(#)strtrimcl
  8. * @(#)      Removes all leading occurences of specific characters in a string.
  9. * @(#)strtrimc
  10. * @(#)      Removes all leading and traling occurences of specific characters
  11. * @(#)      in a string.
  12. *
  13. ***************************************************************************
  14. *@(#)1993 Erik Bachmann
  15. *
  16. * Released to public domain 27-Oct-95
  17. ***************************************************************************/
  18.  
  19. #include <string.h>
  20. #include "bacstd.h"
  21.  
  22. /*
  23.  /-------------------------------------\
  24. |  STRTRIMCR                            |------------------------------------|
  25. |\-------------------------------------/
  26. |
  27. | Removes all trailing occurences of specific characters in a string.
  28. |
  29. |
  30. |----------------------------------------------------------------------------|
  31. | CALL:
  32. |    strtrimcr(&str, ":;\\");
  33. |
  34. | HEADER:
  35. |    ctype.h
  36. |
  37. | GLOBALE VARIABLES:
  38. |    %
  39. |
  40. | ARGUMENTS:
  41. |    pszStr      : String to be converted
  42. |    pszSet      : String with the characters to remove
  43. |
  44. |
  45. | PROTOTYPE:
  46. |    int _CfnTYPE strtrimcr(char *szStr, char *szSet);
  47. |
  48. | RETURN VALUE:
  49. |    j-i         : No of removed characters
  50. |
  51. | MODULE:
  52. |    strtrim.c
  53. |----------------------------------------------------------------------------|
  54. |
  55. |
  56. |----------------------------------------------------------------------------|
  57. |1992-11-09/Erik Bachmann
  58. \---------------------------------------------------------------------------|*/
  59.  
  60. int _CfnTYPE strtrimcr(char *szStr, char *szSet)
  61. {
  62.       int   i, j;                                     /* Locale counters */
  63.  
  64.       /*-------------------------------------------------*/
  65.  
  66.       j = i = strlen(szStr) - 1;                /* Find length of string */
  67.  
  68.       while (strrchr(szSet, szStr[ i ])
  69.                   && (0 <= i))
  70.       {
  71.             /* While string is terminated by one of the specified characters */
  72.             szStr[ i-- ] = '\0';          /*- Replace character with '\0' */
  73.       }
  74.  
  75.       return(j - i);    /* Return the difference between old and new length */
  76. }
  77.  
  78. /*
  79.  /-------------------------------------\
  80. |  STRTRIMCL                            |------------------------------------|
  81. |\-------------------------------------/
  82. |
  83. | Removes all leading occurences of a specific character in a string.
  84. |
  85. |----------------------------------------------------------------------------|
  86. | CALL:
  87. |    strtrimcl(&str, ";:\\");
  88. |
  89. | HEADER:
  90. |    ctype.h
  91. |
  92. | GLOBALE VARIABLES:
  93. |    %
  94. |
  95. | ARGUMENTS:
  96. |    pszStr      : String to be converted
  97. |    pszSet      : String with the characters to remove
  98. |
  99. | PROTOTYPE:
  100. |    int _CfnTYPE strtrimcl(char *szStr, char *szSet);
  101. |
  102. | RETURN VALUE:
  103. |    i           : No of removed characters
  104. |
  105. | MODULE:
  106. |    strtrim.c
  107. |----------------------------------------------------------------------------|
  108. |
  109. |
  110. |----------------------------------------------------------------------------|
  111. |1992-11-09/Erik Bachmann
  112. \---------------------------------------------------------------------------|*/
  113.  
  114. int _CfnTYPE strtrimcl(char *szStr, char *szSet)
  115. {
  116.       int   i = 0, j;
  117.  
  118.       /*-------------------------------------------------*/
  119.  
  120.       j = strlen(szStr) - 1;                    /* Find length of string */
  121.  
  122.       while (strrchr(szSet, szStr[ i ])
  123.                   && (i <= j))
  124.       {
  125.             /* While first character in string matches tag */
  126.  
  127.             i++;                    /*- Count no of removed chars */
  128.       }
  129.  
  130.       if (0 < i)                    /* IF there were matches */
  131.             strcpy(szStr, &szStr[ i ]);         /*- shift string to the left */
  132.  
  133.       return(i);                    /* Return no of matching chars */
  134. }
  135.  
  136. /*
  137.  /-------------------------------------\
  138. |  STRTRIMC                             |------------------------------------|
  139. |\-------------------------------------/
  140. |
  141. | Removes all leading and trailing occurences of a specific character in
  142. |  a string.
  143. |
  144. |----------------------------------------------------------------------------|
  145. | CALL:
  146. |    strtrimc(&str, ";:\\");
  147. |
  148. | HEADER:
  149. |    ctype.h
  150. |
  151. | GLOBALE VARIABLES:
  152. |    %
  153. |
  154. | ARGUMENTS:
  155. |    pszStr      : String to be converted
  156. |    pszSet      : String with the characters to remove
  157. |
  158. | PROTOTYPE:
  159. |    int _CfnTYPE strtrimc(char *szStr, char *szSet);
  160. |
  161. | RETURN VALUE:
  162. |    iStatusFlag : No of removed characters
  163. |
  164. | MODULE:
  165. |    strtrimc.c
  166. |----------------------------------------------------------------------------|
  167. |
  168. |
  169. |
  170. |----------------------------------------------------------------------------|
  171. |1992-11-09/Erik Bachmann
  172. \---------------------------------------------------------------------------|*/
  173.  
  174. int _CfnTYPE strtrimc(char *szStr, char *szSet)
  175. {
  176.       int   iStatusFlag;
  177.  
  178.       /*-------------------------------------------------*/
  179.  
  180.       iStatusFlag =  strtrimcl(szStr, szSet);
  181.       iStatusFlag += strtrimcr(szStr, szSet);
  182.  
  183.       return(iStatusFlag);
  184. }
  185.  
  186. /*
  187.  /-------------------------------------\
  188. |  REP_LAST_CHAR                        |------------------------------------|
  189. |\-------------------------------------/
  190. |
  191. | Replaces the last char on match with another specified char.
  192. |
  193. |
  194. |----------------------------------------------------------------------------|
  195. | CALL:
  196. |    rep_last_char(str, '\n', '\0');
  197. |
  198. | HEADER:
  199. |    string.h
  200. |
  201. | GLOBALE VARIABLES:
  202. |    %
  203. |
  204. | ARGUMENTS:
  205. |    pszStr      : String to be converted
  206. |    cChar1      : Character to replace
  207. |    cChar2      : Character to replace with
  208. |
  209. |
  210. | PROTOTYPE:
  211. |    int _CfnTYPE rep_last_char(char *szStr, char cChar1, char cChar2);
  212. |
  213. | RETURN VALUE:
  214. |    int         : Stringlength
  215. |
  216. | MODULE:
  217. |    strtrim.c
  218. |----------------------------------------------------------------------------|
  219. |
  220. |
  221. |----------------------------------------------------------------------------|
  222. |1992-11-09/Erik Bachmann
  223. \---------------------------------------------------------------------------|*/
  224.  
  225. int _CfnTYPE rep_last_char(char *pszStr, char cChar1, char cChar2)
  226. {
  227.       int   i;
  228.  
  229.       /*--------------------------------*/
  230.  
  231.       i = strlen(pszStr) - 1;
  232.  
  233.       if (pszStr[ i ] == cChar1)
  234.             pszStr[ i ] = cChar2;
  235.  
  236.       return(i);
  237. }     /*** rep_last_char() ***/
  238.  
  239. #ifdef TEST
  240.  
  241. main()
  242. {
  243.       char  *strl = "xxyzxxxLeading x",
  244.                   *strr = "x Traling xxyzxxx",
  245.                   *str  = "xxzyxLead-&trailingxzxyx";
  246.       /*--------------------------------*/
  247.  
  248.       printf("\nBefore convertion:\n\t\"%s\"\n\t\"%s\"\n\t\"%s\"\n",
  249.                   strl, strr, str);
  250.  
  251.       strtrimcr(strr, "xyz");
  252.       strtrimcl(strl, "xyz");
  253.       strtrimc( str, "xyz");
  254.  
  255.       printf("\nAfter convertion:\n\t\"%s\"\n\t\"%s\"\n\t\"%s\"\n",
  256.                   strl, strr, str);
  257.  
  258.       return(0);
  259. }
  260.  
  261. #endif /* TEST */
  262.