Namespaces
Variants
Views
Actions

strrchr

From cppreference.com
< c‎ | string‎ | byte
Revision as of 15:18, 13 December 2012 by P12 (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <string.h>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td class="t-dcl-nopad">
char *strrchr( const char *str, int ch );
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Finds the last occurrence of the character ch in the byte string pointed to by str.

Contents

Parameters

str - pointer to the null-terminated byte string to be analyzed
ch - character to search for

Return value

Pointer to the found character in str, or NULL if no such character is found.

Example

#include <string.h>
#include <stdio.h>
 
int main()
{
    char szSomeFileName[] = "foo/bar/foobar.txt";
    char *pLastSlash = strrchr(szSomeFileName, '/');
    char *pszBaseName = pLastSlash ? pLastSlash + 1 : szSomeFileName;
    printf("Base Name: %s", pszBaseName);
}

Output:

Base Name: foobar.txt

See also

Template:c/string/byte/dcl list strchrTemplate:c/string/byte/dcl list strpbrk
C++ documentation for strrchr