home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / GRLF-C-2.ZIP / GFUNC / STRFIND.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-30  |  1.0 KB  |  61 lines

  1. /*
  2.  * strfind.c
  3.  * contains: strfind()
  4.  *
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "gfuncts.h"
  10.  
  11. /*
  12.  *  int
  13.  * strfind(ps,pt)
  14.  *
  15.  * ARGUMENT
  16.  *  (char *)    ps    -  string to be searched
  17.  *  (char *)    pt    -  string to search for
  18.  *
  19.  * DESCRIPTION
  20.  *  The string is searched for the substring.  If it is found in its
  21.  *  entirety, the position in the specified string (ps) is returned.
  22.  *
  23.  * RETURNS
  24.  *  position of substring or -1 if not found.
  25.  *
  26.  * AUTHOR
  27.  *   Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
  28.  */
  29. int GF_CONV strfind(ps,pt)
  30. char *ps, *pt;
  31. {
  32.     int i,k,itemp,slen,tlen;
  33.  
  34.     if((!(tlen=strlen(pt))) || (tlen>(slen=strlen(ps))) ) 
  35.         return (-1);
  36.     i=k=itemp=0;
  37.     while (1) {
  38.         if (ps[i]==pt[0]) {
  39.             itemp=i;
  40.             while(1) {
  41.                 if((k+1)==tlen) 
  42.                     return itemp;
  43.                 else if((i+k+1)==slen) 
  44.                     return (-1);
  45.                 else {
  46.                     ++k;
  47.                     if(ps[i+k]!=pt[k]) { 
  48.                         k=0;
  49.                         goto point1;
  50.                     }
  51.                 }
  52.             }
  53.         } else {
  54.             if(!ps[i]) 
  55.                 return(-1);
  56. point1:
  57.             ++i;
  58.         }
  59.     }
  60. }
  61.