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

  1. /*
  2. HEADER:         ;
  3. TITLE:          BASIC instr() (in string) function
  4. VERSION:        1.4;
  5.  
  6. DESCRIPTION:    performs BASIC instr() function with similar syntax. This
  7.                 comes in handy when manually converting BASIC to C
  8.  
  9.        syntax: instr(starting_position,string_to_look_in,string_to_find)
  10.                 starting position is 0 to length of line - 1,
  11.                 this function returns 0 to length of line if string was
  12.                 located, or < -1 if not found
  13.  
  14. KEYWORDS:       ;
  15. SYSTEM:         Xenix 3.4b, MSDOS;
  16. FILENAME:       instr.c
  17. WARNINGS:       compile with -dNO_PROTOTYPE if your system does not
  18.                 support prototyping, with -dFOR_MSDOS if you are compiling
  19.                 for MSDOS with an ANSI standard compiler.
  20.                 Defaults assume compiling with prototypes for
  21.                 Xenix 3.4b on Altos 2086 computer.
  22.  
  23. SEE-ALSO:       ;
  24. AUTHORS:        Vern Martin, 449 W. Harrison, Alliance, Ohio 44601;
  25. COMPILERS:      ECOSOFT ECO-C88, XENIX 3.4B STANDARD COMPILER;
  26. */
  27.  
  28. #include "vernmath.h"
  29. #define NOT_FOUND   -1
  30.  
  31. int instr(position,search,find)
  32. int position;
  33. char *search,*find;
  34. {
  35.     int insearch,infind=0,match=0,prefind=0;
  36.     insearch = position;
  37. /*  check for error condition   */
  38.     if (position > (strlen(search)-1)) {
  39.         return(NOT_FOUND);
  40.     }
  41.     while(search[insearch] != (char) NULL &&
  42.         find[infind] != (char) NULL && match == 0) {
  43.         if (search[insearch] != find[infind]) {
  44.             insearch++;
  45.             continue;
  46.         }
  47. /*  record the position of the match of the first char  */
  48.         position = insearch++;
  49. /*  set preliminary find flag to true, preliminary match found */
  50.         prefind = 1;
  51.         infind++;
  52.         while(search[insearch] != (char) NULL &&
  53.             find[infind] != (char) NULL && match == 0) {
  54.             if (search[insearch] != find[infind])  {
  55. /*  do not increment insearch -- it might match the first char of infind */
  56.                 infind = 0;
  57.                 break;
  58.             }
  59.             insearch++;
  60.             infind++;
  61.         }
  62. /*  if you made it to the end of the find string then you have a match */
  63.         if (!find[infind]) match = 1;
  64. /*  if the preliminary find flag is set and a full match has not yet
  65.     been found then set the search posision to one past fist char match */
  66.         if (prefind && !match) {
  67.             prefind = 0;
  68.             insearch = position+1;
  69.         }
  70.     }
  71.     if (match) return(position);
  72.     else return(NOT_FOUND);
  73. }
  74.