std::islower(3)

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

🌸 std::islower(3) 🌸

okay bestie, welcome to std::islower β€” that's the C++ Standard Library version, page 3 of the manual, and she is literally the lowercase detector of your dreams πŸ’…


✨ NAME ✨

std::islower β€” std::islower (yes she's named after herself, main character energy πŸ‘‘)

✨ Synopsis ✨

so first thing, she lives here:

Defined in header <cctype>

and her whole signature is just:

int islower( int ch );

what she does: she checks if the character you hand her is classified as a lowercase character according to the current C locale. πŸŽ€

in the default "C" locale, std::islower returns a nonzero value only for the lowercase letters β€” and those are, exactly:

abcdefghijklmnopqrstuvwxyz

and here's a slay little guarantee for you: if islower returns a nonzero value, then std::iscntrl, std::isdigit, std::ispunct, and std::isspace are all guaranteed to return zero for that same character in that same C locale. no cap. 🫢

⚠️ and now the scary part, bestie: the behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF. like, undefined-undefined. don't do it. 😭

✨ Parameters ✨

  • ch β€” the character to classify πŸ¦‹

✨ Return value ✨

Non-zero value if the character is a lowercase letter, zero otherwise. that's it, that's the whole vibe check ⭐

✨ Notes ✨

like all the other functions from <cctype>, the behavior of std::islower is undefined if the argument's value is neither representable as unsigned char nor equal to EOF. πŸ’€

so to use these functions safely with plain chars (or signed chars), convert the argument to unsigned char first, like this:

bool my_islower(char ch)
{
    return std::islower(static_cast<unsigned char>(ch));
}

same energy with standard algorithms β€” do not use them directly when the iterator's value type is char or signed char. convert the value to unsigned char first instead:

int count_lowers(const std::string& s)
{
    return std::count_if(s.begin(), s.end(),
                      // static_cast<int(*)(int)>(std::islower) // wrong
                      // [](int c){ return std::islower(c); }        // wrong
                      // [](char c){ return std::islower(c); }       // wrong
                         [](unsigned char c){ return std::islower(c); } // correct
                        );
}

see how she left receipts? the wrong ones are commented out and labeled // wrong and the correct one is labeled // correct. iconic. πŸ”₯

✨ Example ✨

// Run this code

#include <cctype>
#include <clocale>
#include <iostream>

int main()
{
    unsigned char c = '\xe5'; // letter Γ₯ in ISO-8859-1

    std::cout << "islower(\'\\xe5\', default C locale) returned "
              << std::boolalpha << (bool)std::islower(c) << '\n';

    std::setlocale(LC_ALL, "en_GB.iso88591");
    std::cout << "islower(\'\\xe5\', ISO-8859-1 locale) returned "
              << std::boolalpha << (bool)std::islower(c) << '\n';

}

πŸ“ Possible output πŸ“

islower('\xe5', default C locale) returned false
islower('\xe5', ISO-8859-1 locale) returned true

see, locale changes everything. she contains multitudes. ✨

✨ See also ✨

  • islower(std::locale) β€” checks if a character is classified as lowercase by a locale (function template) 🌈
  • iswlower β€” checks if a wide character is lowercase (function) 🧚
  • C documentation for islower πŸ’œ

πŸ“Š the ASCII values chart (she's long but she's thorough)

ASCII values  characters  iscntrl isprint isspace isblank isgraph ispunct isalnum isalpha isupper islower isdigit isxdigit  iswcntrl iswprint iswspace iswblank iswgraph iswpunct iswalnum iswalpha iswupper iswlower iswdigit iswxdigit
0–8       \x0–\x8     \0–\10    control codes (NUL, etc.)    β‰ 0  0  0  0  0  0  0  0  0  0  0
9         \x9         \11       tab (\t)                     β‰ 0  0  β‰ 0 β‰ 0  0  0  0  0  0  0  0
10–13     \xA–\xD     \12–\15   whitespaces (\n, \v, \f, \r) β‰ 0  0  β‰ 0  0  0  0  0  0  0  0  0
14–31     \xE–\x1F    \16–\37   control codes                β‰ 0  0  0  0  0  0  0  0  0  0  0
32        \x20        \40       space                         0 β‰ 0  β‰ 0 β‰ 0  0  0  0  0  0  0  0
33–47     \x21–\x2F   \41–\57   !"#$%&'()*+,-./              0 β‰ 0  0  0 β‰ 0 β‰ 0  0  0  0  0  0
48–57     \x30–\x39   \60–\71   0123456789                    0 β‰ 0  0  0 β‰ 0  0 β‰ 0  0  0  0 β‰ 0 β‰ 0
58–64     \x3A–\x40   \72–\100  :;<=>?@                      0 β‰ 0  0  0 β‰ 0 β‰ 0  0  0  0  0  0
65–70     \x41–\x46   \101–\106 ABCDEF                       0 β‰ 0  0  0 β‰ 0  0 β‰ 0 β‰ 0 β‰ 0  0  0 β‰ 0
71–90     \x47–\x5A   \107–\132 GHIJKLMNOPQRSTUVWXYZ         0 β‰ 0  0  0 β‰ 0  0 β‰ 0 β‰ 0 β‰ 0  0  0  0
91–96     \x5B–\x60   \133–\140 [\]^_`                       0 β‰ 0  0  0 β‰ 0 β‰ 0  0  0  0  0  0
97–102    \x61–\x66   \141–\146 abcdef                       0 β‰ 0  0  0 β‰ 0  0 β‰ 0 β‰ 0  0 β‰ 0  0 β‰ 0
103–122   \x67–\x7A   \147–\172 ghijklmnopqrstuvwxyz         0 β‰ 0  0  0 β‰ 0  0 β‰ 0 β‰ 0  0 β‰ 0  0  0
123–126   \x7B–\x7E   \172–\176 {|}~                         0 β‰ 0  0  0 β‰ 0 β‰ 0  0  0  0  0  0
127       \x7F        \177      backspace character (DEL)    β‰ 0  0  0  0  0  0  0  0  0  0  0

and that's the whole page, bestie 🫢 this adorable little reference came from http://cppreference.com, dated 2024.06.10, and it's page std::islower(3). go forth and classify your lowercase letters correctly, and please cast to unsigned char. for me. πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from