.TH rindex() "" "" "String Function (libc)"
.PC "Find rightmost occurrence of a character in a string"
.B "#include <string.h>"
\fBchar *rindex(\fIstring\^\fB, \fIc\^\fB) char *\fIstring\^\fB; char \fIc\^\fB;\fR
.PP
.B rindex()
scans
.I string
for the last occurrence of character
.IR c .
If
.I c
is found,
.B rindex()
returns a pointer to it.
If it is not found,
.B rindex()
returns NULL.
.SH Example
This example uses \fBrindex()\fR to help strip a sample file name
of the path information.
.DM
.ta 0.4i 2.0i
#include <stdio.h>
#include <string.h>
#include <misc.h>
#define PATHSEP '/'		/* path name separator */
.DE
.DM
main()
{
	char *testpath = "/foo/bar/baz";
	printf("Before massaging: %s\en", testpath);
	printf("After massaging: %s\en", basename(testpath));
	return(EXIT_SUCCESS);
}
.DE
.DM
char *basename(path)
char *path;
{
	char *cp;
	return (((cp = rindex(path, PATHSEP)) == NULL)
		 ? path : ++cp);
}
.DE
.SH "See Also"
.Xr "libc," libc
.Xr "strchr()," strchr
.Xr "strrchr()," strrchr
.Xr "string.h" string.h
.SH Notes
You
.I must
include header file
.B string.h
in any program that uses
.BR rindex() ,
or that program will not link correctly.
.PP
.B rindex()
is now obsolete.
You should use
.B strrchr()
instead.
