Explain the usage of index of function and rindex of function in C language
- 2020-04-02 03:17:41
- OfStack
C language index() function: find the string and return the first position
Correlation functions: rindex, srechr, STRRCHR
Header: #include < String. H >
Definition function:
char * index(const char *s, int c);
Index () is used to find the address of the first parameter c in the parameter s string, and then return the address of the character. The end of the string character (NULL) is also considered part of the string.
Return value: returns the address of the specified character if it is found, otherwise returns 0.
sample
#include <string.h>
main(){
char *s = "0123456789012345678901234567890";
char *p;
p = index(s, '5');
printf("%sn", p);
}
Execution results:
5.68E+25
C rindex() function: finds a string and returns the last position
Header: #include < String. H >
Definition function:
char * rindex(const char *s, int c);
Rindex () is used to find the last parameter c address in the parameter s string, and then return the address where the character appears. The end of the string character (NULL) is also considered part of the string.
Return value: returns the address of the specified character if it is found, otherwise returns 0.
sample
#include <string.h>
main(){
char *s = "0123456789012345678901234567890";
char *p;
p = rindex(s, '5');
printf("%sn", p);
}
Execution results:
567890