index(3) CLIX index(3)
NAME
index - Returns the offset of a substring within a string
LIBRARY
Programmer's Workbench Library (libPW.a)
SYNOPSIS
#include <pw.h>
int index(
char *str ,
char *substr );
PARAMETERS
str Contains the string.
substr Contains the substring to be found.
DESCRIPTION
The function described in this reference manual entry is based in the
Programmer's Workbench Library (libPW.a). For information about the
index() function based in the Berkeley Software Distribution Library
(libbsd.a), see the other index(3) reference manual entry.
The index function searches for an occurrence of the substring substr
within the string str.
EXAMPLES
The following example shows a typical use of the index function.
main()
{
int offset;
char str[10] = "ABC123DEF",
substr[4] = "123";
int index();
offset = index(str, substr);
if (offset >= 0)
{
printf("%s appears in %s at character %d\n",
substr, str, offset);
}
else
2/94 - Intergraph Corporation 1
index(3) CLIX index(3)
{
printf("%s does not appear within %s\n", substr, str);
}
}
In this example, the string is ABC123DEF, and the substring is 123. If
the substring is found in the string, a message appears indicating that
the substring appears at a certain character. If the substring is not
found, a message stating that fact is printed. In this example, the
message which appears is 123 appears in ABC123DEF at character 4.
RETURN VALUES
If the substring substr is found within the string str, the offset at
which the substring begins is returned.
If the substring substr does not appear within the string str, the value
-1 is returned.
2 Intergraph Corporation - 2/94