home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / c / cuj9301.zip / 1101074A < prev    next >
Text File  |  1992-11-03  |  3KB  |  124 lines

  1. /*****************************************************
  2.            File Name: STR_NGET.C
  3.          Description: Library of functions for geting
  4.                       substrings in a string
  5. Global Function List: str_nleft
  6.                       str_nmid
  7.                       str_nright
  8.                       str_rstr
  9.          Portability: Standard C
  10. ******************************************************/
  11.  
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <str_nget.h>
  15.  
  16. /*****************************************************
  17.          Name: str_nleft
  18. Expanded Name: Get Left N Characters
  19.    Parameters: Str - string to get left charaters
  20.                Num - number of characters to get
  21.        Return: Str
  22.   Description: Get Num leftmost charcters in Str.
  23.                Modifies Str.
  24. *****************************************************/
  25. char *str_nleft( char *Str, size_t Num )
  26.    {
  27.  
  28.    if ( Num < strlen( Str ))
  29.       {
  30.       Str[Num] = '\0';
  31.       }
  32.  
  33.    return ( Str );
  34.  
  35.    }   /* function str_nleft */
  36.  
  37.  
  38. /*****************************************************
  39.          Name: str_nmid
  40. Expanded Name: Get Middle N Characters
  41.    Parameters: Str - string to get substring in
  42.                Pos - index into Str of start of midstr
  43.                Num - count of charcters to get
  44.        Return: Str
  45.   Description: Get Num chars from middle of string.
  46. *****************************************************/
  47. char *str_nmid( char *Str, size_t Pos, size_t Num )
  48.    {
  49.  
  50.    char *Mid;
  51.    size_t Len = strlen( Str );
  52.  
  53.    if ( Pos >= Len )
  54.       {
  55.       /* Outside of string */
  56.       *Str = '\0';
  57.       return ( Str );
  58.       }
  59.  
  60.    /* Adjust count if it extends outside of string */
  61.    if ( Pos + Num > Len )
  62.       {
  63.       Num = Len - Pos;
  64.       }
  65.  
  66.    Mid = &Str[Pos];
  67.    memmove( (void *)Str, (void *)Mid, Num );
  68.    Str[Num] = '\0';
  69.  
  70.    return ( Str );
  71.  
  72.    }   /* function str_nmid */
  73.  
  74.  
  75. /*****************************************************
  76.          Name: str_nright
  77. Expanded Name: Get Right N Characters
  78.    Parameters: Str - string to get right charaters
  79.                Num - number of characters to get
  80.        Return: Str
  81.   Description: Get Num righmost charcters in Str.
  82.                Modifies Str.
  83. *****************************************************/
  84. char *str_nright( char *Str, size_t Num )
  85.    {
  86.  
  87.    size_t Len = strlen( Str );
  88.  
  89.    return ( str_nmid( Str,
  90.          ( Num > Len ? 0 : Len - Num ),
  91.          min( Num, Len ) ) );
  92.  
  93.    }   /* function str_nright */
  94.  
  95.  
  96. /*****************************************************
  97.          Name: str_rstr
  98. Expanded Name: String Right (Reverse) Search
  99.    Parameters: Str - string to search
  100.                Find - string to search for
  101.        Return: Pointer to last occurance of substring
  102.                Find in Str or NULL if not found
  103.   Description: Searches for last occurrence of sub
  104.                string Find within Str.
  105. *****************************************************/
  106. char *str_rstr( char *Str, char *Find )
  107.    {
  108.  
  109.    char *StrResult = NULL, *StrWork = Str;
  110.  
  111.    while ( ( StrWork =
  112.          strstr( StrWork, Find ) ) != NULL )
  113.       {
  114.       StrResult = StrWork;
  115.       StrWork++;
  116.       }
  117.  
  118.    return ( StrResult );
  119.  
  120.    }   /* function str_rstr */
  121.  
  122. /* End of File */
  123.  
  124.