[net.lang.c] index

ksl@hou2e.UUCP (a hacker) (07/25/85)

What is the BSD function "index" supposed to do?  Does anyone
know the equivalent on a System V Release 2 machine?

Thanks,
hou2e!ksl

larry@kitty.UUCP (Larry Lippman) (07/29/85)

> What is the BSD function "index" supposed to do?  Does anyone
> know the equivalent on a System V Release 2 machine?

I have System V Release 2.2 on 3B2's and have used the index(3F) function
in Fortran applications.  The function is called from a Fortran program and
returns the location (i.e., integer character count) of the substring <cs2>
in the character string <cs1>.  It works like this:

	integer i
	character*len1 cs1
	character*len2 cs2

	i=index(cs1,cs2)

Index is similar to the C language function 'strspn'.
	
I am not a BSD person, so I can't tell you if BSD does anything different
from the above.

	Larry Lippman
	Recognition Research Corp.
	Clarence, New York
	UUCP	{decvax,dual,rocksanne,rocksvax,watmath}!sunybcs!kitty!larry
		{rice,shell}!baylor!kitty!larry
		syr!buf!kitty!larry
	VOICE	716/741-9185
	TELEX	{via WUI} 69-71461 answerback: ELGECOMCLR

	"Have you hugged your cat today?"

mjs@eagle.UUCP (M.J.Shannon) (07/29/85)

> > What is the BSD function "index" supposed to do?  Does anyone
> > know the equivalent on a System V Release 2 machine?

The C function index() from V7 and 4.xBSD maps to strchr() in System V.
Similarly, rindex() maps to strrchr().
-- 
	Marty Shannon
UUCP:	ihnp4!eagle!mjs
Phone:	+1 201 522 6063

guy@sun.uucp (Guy Harris) (07/31/85)

> > What is the BSD function "index" supposed to do?  Does anyone
> > know the equivalent on a System V Release 2 machine?
> 
> I have System V Release 2.2 on 3B2's and have used the index(3F) function
> in Fortran applications.  The function is called from a Fortran program and
> returns the location (i.e., integer character count) of the substring <cs2>
> in the character string <cs1>.

The user was referring to the C library function "index", not the Fortran
function.  The C function searches for a character in a string, not a string
in another string (as the Fortran and PL/I functions of that name do), which
is why I (mildly) approve of the renaming to "strchr" in System III (or
earlier).

> Index is similar to the C language function 'strspn'.

No, it isn't.  "strspn" looks for any of the characters contained in the
second argument string in the first argument string, while the Fortran and
PL/I "index" look for the second argument string in its entirety.

	strspn(<string>, " \t\n")

will return the address of the first blank, tab, or newline in <string>,
while

	index(<string>, " \t\n")

will (in Fortran or PL/I, but NOT in C) look for the first occurrence of a
blank followed by a tab followed by a newline in <string>.

	Guy Harris