C++ strrchr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

In C++, strrchr() searches a C-string for one character and returns the last match. Use it for the final '/' in a path, or the last '.' before a file extension.

Both arguments are required. A match is a pointer into the same string. If the character is missing, the result is nullptr. strchr() stops at the first match. strrchr() keeps the last one. Include <cstring>.

Syntax

Use this when you want the final match, such as the last slash in a path.

C++
strrchr("a/b/c.txt", '/');
CallWhat it does
strrchr("a/b/c.txt", '/')Points at the last / in the path.
Library declarations
C++
const char* strrchr(const char* str, int ch);

Parameters

ParameterDescription
strRequired. The C-string to search.
chRequired. The character to find, written in single quotes.

Return Value

Return ValueDescription
PointerPoints at the last matching character inside the same string.
nullptrThe character is not in the string.

C++ Examples

Example 1 — Basic strrchr()

Find the last letter in a word.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* word = "programming";
    const char* found = std::strrchr(word, 'g');

    if (found != nullptr) {
        std::size_t index = static_cast<std::size_t>(found - word);
        std::cout << "Last 'g' at index: " << index << '\n';
        std::cout << "From there: " << found << '\n';
    }
    return 0;
}

How It Works

  1. programming has 'g' at more than one place.
  2. strrchr returns the rightmost one (index 10).
  3. Pointer subtraction gives a zero-based index.

Example 2 — Base Name from a Path

Use the last slash to get the file name.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* path = "folder/docs/readme.txt";
    const char* slash = std::strrchr(path, '/');

    const char* base = (slash != nullptr) ? slash + 1 : path;
    std::cout << "Base name: " << base << '\n';
    return 0;
}

How It Works

  1. Last '/' sits before readme.txt.
  2. slash + 1 skips the slash and points at the base name.
  3. If there is no slash, use the whole path.

Example 3 — Character Not Found

Missing characters return nullptr.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* text = "CodeToFun";
    const char* found = std::strrchr(text, 'z');

    if (found == nullptr) {
        std::cout << "Character not found\n";
    }
    return 0;
}

How It Works

  1. There is no 'z' in the string.
  2. Result is nullptr.
  3. Always test before dereferencing.

Frequently Asked Questions

strrchr() finds the last time a character appears in a C-string.
str is the C-string to search. ch is the character to find.
A pointer to the last match inside the same string, or nullptr when the character is missing.
strchr() stops at the first match. strrchr() keeps going and returns the last match.
A missing character has no position. Do not use the pointer until you know it found something.

Next: strspn()

Measure how many leading characters belong to an accept set.

strspn() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful