Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | byte
(correction)
m (Example: -'\n')
 
(4 intermediate revisions by 3 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 );
 
}}
 
}}
Line 7: Line 7:
 
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" locale, the following uppercase letters {{tt|ABCDEFGHIJKLMNOPQRSTUVWXYZ}} are replaced with respective lowercase letters {{tt|abcdefghijklmnopqrstuvwxyz}}.
+
In the default {{c|"C"}} locale, the following uppercase letters {{tt|ABCDEFGHIJKLMNOPQRSTUVWXYZ}} are replaced with respective lowercase letters {{tt|abcdefghijklmnopqrstuvwxyz}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | ch | character to be converted. If the value of {{tt|ch}} is not representable as {{c|unsigned char}} nor does not equal {{c|EOF}}, the behavior is undefined}}
+
{{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}}
 
{{par 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===
 
===Notes===
Line 21: Line 21:
  
 
===Example===
 
===Example===
{{example |
+
{{example
| code=
+
|code=
#include <iostream>
+
 
#include <cctype>
 
#include <cctype>
 
#include <clocale>
 
#include <clocale>
 +
#include <iostream>
  
 
int main()
 
int main()
 
{
 
{
 
     unsigned 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
 
}}
 
}}
 
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/byte/dsc toupper}}
+
{{dsc inc|cpp/string/byte/dsc toupper}}
{{dsc inc | cpp/locale/dsc tolower}}
+
{{dsc inc|cpp/locale/dsc tolower}}
{{dsc inc | cpp/string/wide/dsc towlower}}
+
{{dsc inc|cpp/string/wide/dsc towlower}}
{{dsc see c | c/string/byte/tolower}}
+
{{dsc see c|c/string/byte/tolower}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/string/byte/tolower]]
+
===External links===
[[es:cpp/string/byte/tolower]]
+
{{elink begin}}
[[fr:cpp/string/byte/tolower]]
+
{{elink|num=1|{{enwiki|ISO/IEC 8859-1}}. From Wikipedia.}}
[[it:cpp/string/byte/tolower]]
+
{{elink|num=2|{{enwiki|ISO/IEC 8859-15}}. From Wikipedia.}}
[[ja:cpp/string/byte/tolower]]
+
{{elink end}}
[[pl:cpp/string/byte/tolower]]
+
 
[[pt:cpp/string/byte/tolower]]
+
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}
[[ru:cpp/string/byte/tolower]]
+
[[zh:cpp/string/byte/tolower]]
+

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.