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

  1. /*
  2. HEADER:         ;
  3. TITLE:          BASIC like mid() string function
  4. VERSION:        1.0;
  5.  
  6. DESCRIPTION:    provides a mid() function like BASIC mid$() with similar
  7.                 syntax, this comes in handy when converting BASIC code
  8.                 to C manually.
  9.                 printf("%s\n",mid(string,10,0)); this prints the entire
  10.                     string beginning with string[ 10 ], since the
  11.                     end limit was not given (that is 0)
  12.                 printf("%s\n",mid(string,2,5)) prints the string from
  13.                     string[ 2 ] to string [ 5 ]
  14.                 A pointer to a static buffer is returned and null
  15.                     terminated. The original string of chars remains
  16.                     unaffected
  17.                 On error, this function returns an empty string (first
  18.                     char is null), that is: ""
  19.  
  20. KEYWORDS:       BASIC,mid,string;
  21. SYSTEM:         Xenix 3.4b, MSDOS;
  22. FILENAME:       mid.c
  23. WARNINGS:       compile with -dNO_PROTOTYPE if your system does not
  24.                 support prototyping, with -dFOR_MSDOS if you are compiling
  25.                 for MSDOS with an ANSI standard compiler.
  26.                 Defaults assume compiling with prototypes for
  27.                 Xenix 3.4b on Altos 2086 computer.
  28.  
  29. SEE-ALSO:       demo.c;
  30. AUTHORS:        Vern Martin, 449 W. Harrison, Alliance, Ohio 44601;
  31. COMPILERS:      ECOSOFT ECO-C88, XENIX 3.4B STANDARD COMPILER;
  32. */
  33.  
  34. #include "vernmath.h"
  35. #define LINEIN 255  /* maximum line length to be handled */
  36. #define nl()    retstring[0] = (char) NULL
  37.  
  38. char *mid(string,first,last)
  39. char *string;
  40. int first,last;
  41. {
  42. /* local char */
  43.     static char retstring[LINEIN+1];
  44. /* local int */
  45.     int x,y,len;
  46.     len = strlen(string);
  47.  
  48. /* check for overflow */
  49.     if (len > LINEIN) {
  50.         nl();
  51.         return(retstring);
  52.     }
  53.  
  54. /* make sure first and last set to proper values */
  55.     if ( first > len-1) {
  56.         nl();
  57.         return ( retstring );
  58.     }
  59.     if ( last > len-1 || last <= 0 ) last = len-1;
  60.     if ( first < 0 ) first = 0;
  61.  
  62. /* copy result into return buffer */
  63.     for ( x = first,y=0; x <= last; x++,y++) retstring[y] = string[x];
  64.     retstring[y] = (char) NULL;
  65.  
  66.     return(retstring);
  67. }
  68.