Name
index, rindex
- locate character in string
Library
libc.lib
Synopsis
|
char *
index (const char *s, int c);
|
|
char *
rindex (const char *s, int c);
|
Return values
The functions
index
and
rindex
return a pointer to the located character, or
NULL
if the character does not appear in the string.
Detailed description
The
index
function
locates the first occurrence of
c
(converted to a
char )
in the string pointed to by
s.
The terminating null character is considered part of the string;
therefore if
c
is
‘\0,’
the functions locate the terminating
‘\0.’
The
rindex
function is identical to
index,
except it locates the last occurrence of
c.
Examples
#include <string.h>
#include <stdio.h>
int main()
{
char one[50];
char* ret;
strcpy(one,"abcd");
ret = index(one, ’c’);
if(!strncmp(one+2,ret,1)) printf("index of \ ’c\ ’ in string \"abcd\" is %d \n",2);
ret = index(one, ’z’);
if(ret == NULL) printf("\ ’z\ ’ not found in string \"abcd\"\n");
ret = index(one, ’\0’);
if(!strncmp(one+4,ret,1)) printf("index of \ ’\ \0\ ’ in string \"abcd\" is %d\n",4);
strcpy(one,"cdcab");
ret = rindex(one, ’c’);
if(!strncmp(one+2,ret,1)) printf("rindex of \ ’c\ ’ in string \"cscab\" is %d\n",2);
strcpy(one,"dcab");
ret = rindex(one, ’\0’);
if(!strncmp(one+4,ret,1)) printf("index of \ ’\ \0\ ’ in string \"dcab\" is %d\n",4);
return 0;
}
Output
index of ’c’ in string "abcd" is 2
’z’ not found in string "abcd"
index of ’\0’ in string "abcd" is 4
rindex of ’c’ in string "cscab" is 2
index of ’\0’ in string "dcab" is 4
See also
memchr,
strchr,
strcspn,
strpbrk,
strsep,
strspn,
strstr,
strtok
Feedback
For additional information or queries on this page send feedback
© 2005-2007 Nokia
|
|