Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/byte/tolower"

From cppreference.com
< cpp‎ | string‎ | byte
(mention locale-specific nature, and example)
m (Example: -'\n')
 
(15 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 
{{cpp/title|tolower}}
 
{{cpp/title|tolower}}
 
{{cpp/string/byte/navbar}}
 
{{cpp/string/byte/navbar}}
{{ddcl | header=cctype |
+
{{ddcl|header=cctype|
 
int tolower( int ch );
 
int tolower( int ch );
 
}}
 
}}
  
 
Converts the given character to lowercase according to the character conversion rules defined by the currently installed C locale.
 
Converts the given character to lowercase according to the character conversion rules defined by the currently installed C locale.
 +
 +
In the default {{c|"C"}} locale, the following uppercase letters {{tt|ABCDEFGHIJKLMNOPQRSTUVWXYZ}} are replaced with respective lowercase letters {{tt|abcdefghijklmnopqrstuvwxyz}}.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | ch | character to be converted}}
+
{{par|ch|character to be converted. If the value of {{c|ch}} is not representable as {{c/core|unsigned char}} and does not equal {{c|EOF}}, the behavior is undefined}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
Lowercase version of {{tt|ch}} or unmodified {{tt|ch}} if no lowercase version is listed in the current C locale.
+
Lowercase version of {{c|ch}} or unmodified {{c|ch}} if no lowercase version is listed in the current C locale.
 +
 
 +
===Notes===
 +
{{cpp/string/byte/note unsigned char}}
  
 
===Example===
 
===Example===
{{example |
+
{{example
| code=
+
|code=
#include <iostream>
+
 
#include <cctype>
 
#include <cctype>
 
#include <clocale>
 
#include <clocale>
 +
#include <iostream>
  
 
int main()
 
int main()
 
{
 
{
     char c = '\xb4'; // the character Ž in ISO-8859-15
+
     unsigned char c = '\xb4'; // the character Ž in ISO-8859-15
                    // but ´ (acute accent) in ISO-8859-1  
+
                              // but ´ (acute accent) in ISO-8859-1
  
 
     std::setlocale(LC_ALL, "en_US.iso88591");
 
     std::setlocale(LC_ALL, "en_US.iso88591");
 
     std::cout << std::hex << std::showbase;
 
     std::cout << std::hex << std::showbase;
     std::cout << "in iso8859-1, tolower('0xb4') gives "
+
     std::cout << "in iso8859-1, tolower('0xb4') gives " << std::tolower(c) << '\n';
              << std::tolower(c) << '\n';
+
 
     std::setlocale(LC_ALL, "en_US.iso885915");
 
     std::setlocale(LC_ALL, "en_US.iso885915");
     std::cout << "in iso8859-15, tolower('0xb4') gives "
+
     std::cout << "in iso8859-15, tolower('0xb4') gives " << std::tolower(c) << '\n';
              << std::tolower(c) << '\n';
+
 
}
 
}
| output=
+
|p=true
 +
|output=
 
in iso8859-1, tolower('0xb4') gives 0xb4
 
in iso8859-1, tolower('0xb4') gives 0xb4
 
in iso8859-15, tolower('0xb4') gives 0xb8
 
in iso8859-15, tolower('0xb4') gives 0xb8
Line 41: Line 45:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/string/byte/dcl list toupper}}
+
{{dsc inc|cpp/string/byte/dsc toupper}}
{{dcl list template | cpp/locale/dcl list tolower}}
+
{{dsc inc|cpp/locale/dsc tolower}}
{{dcl list template | cpp/string/wide/dcl list towlower}}
+
{{dsc inc|cpp/string/wide/dsc towlower}}
{{dcl list see c | c/string/byte/tolower}}
+
{{dsc see c|c/string/byte/tolower}}
{{dcl list end}}
+
{{dsc end}}
 +
 
 +
===External links===
 +
{{elink begin}}
 +
{{elink|num=1|{{enwiki|ISO/IEC 8859-1}}. From Wikipedia.}}
 +
{{elink|num=2|{{enwiki|ISO/IEC 8859-15}}. From Wikipedia.}}
 +
{{elink end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}

Latest revision as of 22:54, 27 June 2024

Defined in header <cctype>
int tolower( int ch );

Converts the given character to lowercase according to the character conversion rules defined by the currently installed C locale.

In the default "C" locale, the following uppercase letters ABCDEFGHIJKLMNOPQRSTUVWXYZ are replaced with respective lowercase letters abcdefghijklmnopqrstuvwxyz.

Contents

[edit] Parameters

ch - character to be converted. If the value of ch is not representable as unsigned char and does not equal EOF, the behavior is undefined

[edit] Return value

Lowercase version of ch or unmodified ch if no lowercase version is listed in the current C locale.

[edit] Notes

Like all other functions from <cctype>, the behavior of std::tolower 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:

char my_tolower(char ch)
{
    return static_cast<char>(std::tolower(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:

std::string str_tolower(std::string s)
{
    std::transform(s.begin(), s.end(), s.begin(),
                // static_cast<int(*)(int)>(std::tolower)         // wrong
                // [](int c){ return std::tolower(c); }           // wrong
                // [](char c){ return std::tolower(c); }          // wrong
                   [](unsigned char c){ return std::tolower(c); } // correct
                  );
    return s;
}

[edit] Example

#include <cctype>
#include <clocale>
#include <iostream>
 
int main()
{
    unsigned char c = '\xb4'; // the character Ž in ISO-8859-15
                              // but ´ (acute accent) in ISO-8859-1
 
    std::setlocale(LC_ALL, "en_US.iso88591");
    std::cout << std::hex << std::showbase;
    std::cout << "in iso8859-1, tolower('0xb4') gives " << std::tolower(c) << '\n';
    std::setlocale(LC_ALL, "en_US.iso885915");
    std::cout << "in iso8859-15, tolower('0xb4') gives " << std::tolower(c) << '\n';
}

Possible output:

in iso8859-1, tolower('0xb4') gives 0xb4
in iso8859-15, tolower('0xb4') gives 0xb8

[edit] See also

converts a character to uppercase
(function) [edit]
converts a character to lowercase using the ctype facet of a locale
(function template) [edit]
converts a wide character to lowercase
(function) [edit]
C documentation for tolower

[edit] External links

1.  ISO/IEC 8859-1. From Wikipedia.
2.  ISO/IEC 8859-15. From Wikipedia.