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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  STRDEL.C - Removes specified characters from a string
  5. **
  6. **  public domain demo by Bob Stout
  7. **
  8. **  NOTE: The name of this funtion violates ANSI/ISO 9899:1990 sec. 7.1.3,
  9. **        but this violation seems preferable to either violating sec. 7.13.8
  10. **        or coming up with some hideous mixed-case or underscore infested
  11. **        naming. Also, many SNIPPETS str---() functions duplicate existing
  12. **        functions which are supported by various vendors, so the naming
  13. **        violation may be required for portability.
  14. */
  15.  
  16. #include <string.h>
  17. #include "snip_str.h"
  18.  
  19. #if defined(__cplusplus) && __cplusplus
  20.  extern "C" {
  21. #endif
  22.  
  23. char *strdel(char *str, size_t posn, size_t len)
  24. {
  25.       char *pos0, *pos1;
  26.  
  27.       if (str)
  28.       {
  29.             if (posn < strlen(str))
  30.             {
  31.                   for (pos0 = pos1 = str + posn;
  32.                         *pos1 && len;
  33.                         ++pos1, --len)
  34.                   {
  35.                         ;
  36.                   }
  37.                   strMove(pos0, pos1);
  38.             }
  39.       }
  40.       return str;
  41. }
  42.  
  43. #if defined(__cplusplus) && __cplusplus
  44.  }
  45. #endif
  46.  
  47. #ifdef TEST
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51.  
  52. main(int argc, char *argv[])
  53. {
  54.       int pos, len;
  55.  
  56.       if (4 > argc)
  57.       {
  58.             puts("Usage: STRDEL string pos len");
  59.             puts("Deletes 'len' characters starting at position 'pos'");
  60.             return EXIT_FAILURE;
  61.       }
  62.       pos = atoi(argv[2]);
  63.       len = atoi(argv[3]);
  64.       printf("strdel(\"%s\", %d, %d) => ", argv[1], pos, len);
  65.       printf("\"%s\"\n", strdel(argv[1], pos, len));
  66.       return EXIT_SUCCESS;
  67. }
  68.  
  69. #endif /* TEST */
  70.