std::strchr(3)
π 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 ofstd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms