home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / STSINDEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  1.9 KB  |  62 lines

  1. /**
  2. *
  3. * Name        stsindex -- Returns the position of the first occurrence
  4. *        of one string within another.
  5. *
  6. * Synopsis    index = stsindex(pcheck,psearch);
  7. *
  8. *        int index      The position of pcheck within psearch
  9. *        char *pcheck      Pointer to the string to searched for
  10. *        char *psearch      Pointer to the string to be searched
  11. *
  12. * Description    STSINDEX searches the string psearch for the string
  13. *        pcheck, returning the position of first occurrence.  If
  14. *        the string does not occur, -1 is returned.  If the
  15. *        length of pcheck is zero, 0 is returned.
  16. *
  17. *        See also STSCHIND.
  18. *
  19. * Returns    index          The position of pcheck in psearch
  20. *
  21. * Version    3.0 (C)Copyright Blaise Computing Inc.    1983, 1984, 1986
  22. *
  23. **/
  24.  
  25. #include <string.h>
  26.  
  27. #include <bstring.h>
  28.  
  29. int stsindex(pcheck,psearch)
  30. register char *pcheck;
  31. char          *psearch;
  32. {
  33.     int  lenc,pos,fpos;
  34.     register int j;
  35.     register char *ptemp;
  36.  
  37.     if ((lenc = (int) strlen(pcheck)) == 0)
  38.        return(0);          /* pcheck has length zero           */
  39.  
  40.     /* Search psearch, starting where the first character of pcheck is */
  41.     /* found.  If the string is not found, find the next possible      */
  42.     /* position for a match.  If a match is found, return the position */
  43.     /* where the string starts; otherwise -1.                   */
  44.  
  45.     fpos = 0;
  46.     while (((int) strlen(psearch)) >= lenc)
  47.     {
  48.        if ((pos = stschind(*pcheck,psearch)) < 0)
  49.       break;               /* First character not found    */
  50.        fpos += pos;
  51.        ptemp = psearch + pos;           /* String to search           */
  52.        for (j = 0; (j < lenc) && (*pcheck++ == *ptemp++); j++);
  53.        if (j == lenc)
  54.        return(fpos);      /* String found               */
  55.        pcheck -= j + 1;       /* Restore pointer to string to check*/
  56.        psearch += pos + 1;      /* Look for first character of pcheck*/
  57.        fpos++;              /* at the next position.           */
  58.     }
  59.  
  60.     return(-1);           /* String not found               */
  61. }
  62.