home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c222 / 1.ddi / SOURCE / CLIB / ST_SUB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-10  |  1.9 KB  |  90 lines

  1. /*********************
  2.  *
  3.  *  st_sub.c - string substring functions.
  4.  *
  5.  *  Purpose: This file contains string substring functions for
  6.  *           extracting parts of a given string.
  7.  *
  8.  *  Blackstar C Function Library
  9.  *  (c) Copyright 1985,1989 Sterling Castle Software
  10.  *
  11.  *******/
  12.  
  13. #include <string.h>
  14. #include "blackstr.h"
  15.  
  16.  
  17. /********
  18.  *
  19.  *   st_subc(string,c,subc) - substitute a character in a string
  20.  *
  21.  **/
  22.  
  23. char *st_subc(
  24.     char *string,           /* string to sub in */
  25.     char c,                 /* character to sub for */
  26.     char subc)              /* char to sub */
  27. {
  28.     char *ptr;
  29.     ptr = string;
  30.     while(*string) {
  31.     if(*string==c)
  32.         *string = subc;
  33.     ++string;
  34.     }
  35.     return(ptr);
  36. }
  37.  
  38.  
  39. /********
  40.  *
  41.  *   st_left(str1,str2,n) - extract left n characters of str2 to str1
  42.  *
  43.  **/
  44.  
  45. char *st_left(
  46.     char *str1,             /* string extracted */
  47.     char *str2,             /* string to extract from */
  48.     short int n)            /* # characters to extract */
  49. {
  50.     return(strncpy (str1,str2,n));
  51. }
  52.  
  53.  
  54. /********
  55.  *
  56.  *   st_right(str1,str2,n) -  extract right n characters of str2 to str1
  57.  *
  58.  **/
  59.  
  60. char *st_right(
  61.     char *str1,             /* string extracted */
  62.     char *str2,             /* string to extract from */
  63.     short int n)            /* # characters to extract */
  64. {
  65.     while(*str2)
  66.     ++str2;         /* go to end of string 2 */
  67.     while(n--)
  68.     --str2;         /* and back up n */
  69.     return(strcpy (str1,str2));
  70. }
  71.  
  72.  
  73. /********
  74.  *
  75.  *   st_substr(str1,str2,pos,n) - extract str1 from str2 at pos for n
  76.  *
  77.  **/
  78.  
  79. char *st_substr(
  80.     char *str1,             /* string extracted */
  81.     char *str2,             /* string to extract from */
  82.     short int pos,          /* starting position (from left) in str2 */
  83.     short int n)            /* # bytes to extract */
  84. {
  85.     while(pos--)
  86.     ++str2;                 /* go to starting position in str2 */
  87.     return(strncpy(str1,str2,n));   /* copy n bytes */
  88. }
  89.  
  90.