std::strchr(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::strchr(3) β€” C++ Standard Library πŸ’–

okay bestie, welcome to the <cstring> chapter of the group chat, the manual page is literally called std::strchr and yes it does exactly what the name screams. sit down, hydrate, let's go 🫢


✨ NAME ✨

std::strchr β€” it finds a character in a string. that's it. that's the whole vibe. πŸ’…

🌸 Synopsis 🌸

first things first, this cutie lives in the header <cstring> β€” don't forget to include it or the compiler will simply not know her:

const char* strchr( const char* str, int ch );
    char* strchr(       char* str, int ch );

so here's the tea: strchr finds the first occurrence of the character static_cast<char>(ch) in the byte string pointed to by str. like, first match, no searching backwards, we move forward only. main character energy πŸŽ€

and the iconic detail β€” the terminating null character is considered to be a part of the string and can be found if you're searching for '\0'. so yes, you can hunt down the null terminator on purpose. it's giving "the end is also a destination". lowkey obsessed 😭

πŸ¦‹ Parameters πŸ¦‹

  • str β€” pointer to the null-terminated byte string to be analyzed. they're the one getting searched, be nice to them.
  • ch β€” character to search for. this is your target, your person, your 'T' if you will.

πŸ‘‘ Return value πŸ‘‘

you get a pointer to the found character in str β€” iconic, precise, exactly where you wanted it. OR a null pointer if no such character is found, which is the polite way of saying "she's not here, bestie" πŸ’€

🌈 Example 🌈

// Run this code β€” fr fr, this one is a classic:

#include <cstring>
#include <iostream>

int main()
{
    const char* str = "Try not. Do, or do not. There is no try.";
    char target = 'T';
    const char* result = str;

    while ((result = std::strchr(result, target)) != nullptr)
    {
        std::cout << "Found '" << target
                  << "' starting at '" << result << "'\n";

        // Increment result, otherwise we'll find target at the same location
        ++result;
    }
}

note the ++result β€” that's the whole trick. without it you'd find the same target at the same location forever and ever, an infinite loop, a cursed timeline πŸ™ˆ advance the pointer, keep it moving.

⭐ Output ⭐

Found 'T' starting at 'Try not. Do, or do not. There is no try.'
Found 'T' starting at 'There is no try.'

look at that. two hits. slay πŸ”₯

πŸ’œ See also πŸ’œ

  • memchr β€” searches an array for the first occurrence of a character (function)
  • find β€” finds the first occurrence of the given substring (public member function of std::basic_string<CharT,Traits,Allocator>)
  • wcschr β€” finds the first occurrence of a wide character in a wide string (function)
  • strrchr β€” finds the last occurrence of a character (function)
  • strpbrk β€” finds the first location of any character from a set of separators (function)
  • C documentation for strchr β€” for when you want the origin story

okay that's the whole page, you're officially fluent 🧚 from http://cppreference.com, dated 2024.06.10, page std::strchr(3). go forth and find your characters, bestie πŸ’–βœ¨

πŸ“¦ where this page comes from