www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/11/22/18:06:09

From: Nate Eldredge <neldredge AT hmc DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: indexOf function
Date: 22 Nov 1999 14:08:17 -0800
Organization: InterWorld Communications
Lines: 23
Message-ID: <8366yu18oe.fsf@mercury.st.hmc.edu>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 991122131030 DOT 4922B-100000 AT is> <81cafs$6m8 AT hermes DOT acs DOT unt DOT edu>
NNTP-Posting-Host: mercury.st.hmc.edu
X-Trace: nntp1.interworld.net 943308562 83752 134.173.45.219 (22 Nov 1999 22:09:22 GMT)
X-Complaints-To: usenet AT nntp1 DOT interworld DOT net
NNTP-Posting-Date: 22 Nov 1999 22:09:22 GMT
X-Newsreader: Gnus v5.7/Emacs 20.4
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Morpheus" <hall AT cs DOT unt DOT edu> writes:

> Could I use some pointer arithmetic to find the index once I use strstr? If
> so, I'm not exactly sure how I'd do it. Could you demonstrate?

Sure.  Untested but probably correct code follows:

ssize_t indexOf(const char *haystack, const char *needle)
{
  const char *p;
  p = strstr(haystack, needle);
  if (p)
    return p - haystack;
  else
    return -1;
}

HTH

-- 

Nate Eldredge
neldredge AT hmc DOT edu

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019