index

Guy Harris guy at sun.uucp
Wed Jul 31 11:38:57 AEST 1985


> > 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



More information about the Comp.lang.c mailing list