home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / STCMID.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  2.0 KB  |  68 lines

  1. /**
  2. *
  3. * Name        stcmid -- Replace characters in the middle of a string.
  4. *
  5. * Synopsis    amov = stcmid(ptarget,psource,cpos,cnt,tarsize)
  6. *
  7. *        int amov      Actual number of characters replaced
  8. *        char *ptarget      Pointer to string whose characters
  9. *                  are to be replaced.
  10. *        char *psource      Pointer to source string
  11. *        int cpos      Position within ptarget where replace-
  12. *                  ment begins
  13. *        int cnt       Number of characters of psource to
  14. *                  move into ptarget
  15. *        int tarsize      Maximum length of the target string
  16. *
  17. * Description    STCMID replaces cnt characters of ptarget with the
  18. *        characters of psource starting at character position
  19. *        cpos (the first character is at position 0).  If cpos is
  20. *        greater than the length of ptarget, the characters of
  21. *        psource are concatenated to ptarget.  If cnt is less
  22. *        than the length of psource, the leftmost characters of
  23. *        psource are used; if cnt is greater than the length of
  24. *        psource, the remaining positions are filled with blanks.
  25. *        In any case, no more characters are transferred than the
  26. *        maximum length of ptarget permits.
  27. *
  28. * Returns    amov          The number of characters extracted
  29. *        ptarget       The altered string
  30. *
  31. * Version    3.0 (C)Copyright Blaise Computing Inc.    1983, 1984, 1986
  32. *
  33. **/
  34.  
  35. #include <string.h>
  36.  
  37. #include <bstring.h>
  38.  
  39. #define BLANK     ' '
  40.  
  41. int stcmid(ptarget,psource,cpos,cnt,tarsize)
  42. register char *ptarget,*psource;
  43. int          cpos,cnt,tarsize;
  44. {
  45.     register int i;
  46.     int npos,n,tlen,len;
  47.  
  48.     /* First set up reasonable values for the number of characters and */
  49.     /* the position within ptarget.                       */
  50.  
  51.     tlen = (int) strlen(ptarget);
  52.     npos = min(max(0,cpos),tlen);
  53.     n     = min(cnt,tarsize - npos);
  54.     len  = (int) strlen(psource);
  55.  
  56.     ptarget += npos;
  57.     for (i = 1; i <= n; i++)           /* Copy the string           */
  58.     if (i > len)
  59.        *ptarget++ = BLANK;
  60.     else
  61.        *ptarget++ = *psource++;
  62.  
  63.     if ((npos + n) >= tlen)
  64.        *ptarget = '\0';                /* String extension occurred.   */
  65.  
  66.     return(i - 1);
  67. }
  68.