std::isalpha(3)

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

๐ŸŒธ std::isalpha(3) โ€” C++ Standard Libary ๐ŸŒธ

okay bestie, welcome to the std::isalpha(3) page, straight from the C++ Standard Libary (yes, thatโ€™s the spelling on the page, and weโ€™re keeping it ๐Ÿ’…). This is the alphabet-checking icon of <cctype>, and weโ€™re gonna get through every flag, every caveat, and the entire chaotic ASCII table without losing a single crumb. ๐Ÿ“


๐Ÿ’– NAME ๐Ÿ’–

std::isalpha - std::isalpha

(no cap, the NAME section is literally the name twice. main character energy.)


โœจ Synopsis โœจ

Defined in header <cctype>:

int isalpha( int ch );

Checks if the given character is an alphabetic character as classified by the currently installed C locale. In the default locale, the following characters are alphabetic:

  • uppercase letters ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • lowercase letters abcdefghijklmnopqrstuvwxyz

In locales other than "C", an alphabetic character is a character for which std::isupper() or std::islower() returns non-zero or any other character considered alphabetic by the locale. In any case, std::iscntrl(), std::isdigit(), std::ispunct() and std::isspace() will return zero for this character.

โš ๏ธ The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF. Like, literally undefined โ€” do not play with that. ๐Ÿ˜ญ

๐ŸŒˆ Parameters ๐ŸŒˆ

  • ch โ€” character to classify

๐ŸŽ€ Return value ๐ŸŽ€

Non-zero value if the character is an alphabetic character, zero otherwise. Thatโ€™s it, thatโ€™s the whole vibe check. โœจ


๐Ÿฆ‹ Notes ๐Ÿฆ‹

Like all other functions from <cctype>, the behavior of std::isalpha is undefined if the argumentโ€™s value is neither representable as unsigned char nor equal to EOF. To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char:

bool my_isalpha(char ch)
{
    return std::isalpha(static_cast<unsigned char>(ch));
}

Similarly, they should not be directly used with standard algorithms when the iteratorโ€™s value type is char or signed char. Instead, convert the value to unsigned char first:

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

๐Ÿ‘‘ Example ๐Ÿ‘‘

Demonstrates the use of std::isalpha with different locales (OS-specific).

// Run this code

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

int main()
{
    unsigned char c = '\xdf'; // German letter รŸ in ISO-8859-1

    std::cout << "isalpha(\'\\xdf\', default C locale) returned "
              << std::boolalpha << !!std::isalpha(c) << '\n';

    std::setlocale(LC_ALL, "de_DE.iso88591");
    std::cout << "isalpha(\'\\xdf\', ISO-8859-1 locale) returned "
              << static_cast<bool>(std::isalpha(c)) << '\n';

}

๐Ÿ’ฌ Possible output ๐Ÿ’ฌ

isalpha('\xdf', default C locale) returned false
isalpha('\xdf', ISO-8859-1 locale) returned true

๐ŸŒŸ See also ๐ŸŒŸ

  • isalpha(std::locale) โ€” checks if a character is classified as alphabetic by a locale (function template)
  • iswalpha โ€” checks if a wide character is alphabetic (function)
  • C documentation for isalpha

๐Ÿงฎ ASCII values (the whole entire table) ๐Ÿงฎ

Bestie, this table is a lot, but every crumb stays. The original column headings are, in order:

ASCII values, characters, iscntrl, isprint, isspace, isblank, isgraph, ispunct, isalnum, isalpha, isupper, islower, isdigit, isxdigit, decimal, hexadecimal, octal, iswcntrl, iswprint, iswspace, iswblank, iswgraph, iswpunct, iswalnum, iswalpha, iswupper, iswlower, iswdigit, iswxdigit.

Then each row below is the row content exactly as the page gives it, in order โ€” no crumbs dropped, no values invented. ๐Ÿซถ

  • 0โ€“8, \x0โ€“\x8, \0โ€“\10, control codes (NUL, etc.), โ‰ 0, 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, 0
  • 10โ€“13, \xAโ€“\xD, \12โ€“\15, whitespaces (\n, \v, \f, \r), โ‰ 0, 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, 0
  • 32, \x20, \40, space, 0, โ‰ 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, 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, 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, characters: [, \, ], ^, _, `; flags: 0, โ‰ 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, 0
  • 127, \x7F, \177, backspace character (DEL), โ‰ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

xoxo bestie ๐Ÿ’– โ€” source/version: http://cppreference.com, date 2024.06.10, page std::isalpha(3). periodt. ๐Ÿซถ

๐Ÿ“ฆ where this page comes from