home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 344_01 / cbstrgs.c < prev    next >
Text File  |  1990-02-21  |  2KB  |  82 lines

  1. /*
  2. HEADER:        ;
  3. TITLE:        BASIC-like string functions;
  4. VERSION:    1.0;
  5.  
  6. DESCRIPTION:    Functions in C to implement the BASIC string functions MID$
  7.         and RIGHT$;
  8.  
  9. KEYWORDS:    String utilities;
  10. SYSTEM:        MSDOS;
  11. FILENAME:    CBstrgs;
  12. WARNINGS:    None;
  13. SEE ALSO:    Wgets;
  14.  
  15. AUTHORS:    Dr. Ronald J. Terry;
  16. COMPILERS:    Turbo C;
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20.  
  21. /***************************************************************************
  22.  *                             Function: Mid                               *
  23.  *          Mid selects 'nofchar' characters beginning at 'start'          *
  24.  ***************************************************************************/
  25.  
  26. char *Mid(char *str, int start, int nofchar)
  27. {
  28.      int strgleng = strlen(str), stop;
  29.      char *newstr, *newstr2;
  30.      newstr = calloc(strgleng+1,sizeof(char));
  31.      newstr2 = newstr;
  32.      if(start<1 || nofchar<1 || start>strgleng)
  33.        return(newstr=NULL);
  34.      --start;
  35.      if((nofchar+start)>strgleng)
  36.        nofchar = strgleng - start;
  37.      stop = start + nofchar;
  38.      while(start<stop)
  39.      {
  40.      *newstr++ = *(str + start);
  41.      ++start;
  42.      }
  43.      *newstr = NULL;
  44.      newstr = newstr2;
  45.      return(newstr);
  46. }
  47. /***************************************************************************
  48.  *                Function: Right                   *
  49.  *                 Right selects 'nofchar' rightmost characters            *
  50.  ***************************************************************************/
  51.  
  52. char *Right(char *str, int nofchar)
  53.  
  54. {
  55.      int start;
  56.      char *newstr;
  57.      start = strlen(str) - nofchar +1;
  58.      newstr = Mid(str,start,nofchar);
  59.      return(newstr);
  60. }
  61. /***************************************************************************
  62.  *                      Function: Wgets                            *
  63.  * Gets a string of characters from 'stdin' and echoes to current viewport *
  64.  ***************************************************************************/
  65. char *Wgets(char *s)
  66. {
  67.      char *s2;
  68.      int keys;
  69.      s2 = s;
  70.      while((keys=getche())!=13)
  71.      {
  72.        *s++ = (char) keys;
  73.        if(keys==8)
  74.        {
  75.      putch(32);
  76.      putch(keys);
  77.      s-=2;
  78.        }
  79.      }
  80.      *s = '\0';
  81.      return(s2);
  82. }