Prompt a function that converts a string to lower case.

  • you can obobuvat kozhen symbol із lines function tolower () - sudo97
  • one
    Tse vzhe bulo kazano - skegg

4 answers 4

It seems there is no such function, you can go through all the characters of the string, and apply tolower () to them. Or instead of tolower () use add subtract:

char A; ... //initialization A = A - 'A' + 'a'; // tolower; A = tolower(A); // tolower from ctype.h 
  • "Add subtract" - did not understand the idea. Explain, please. - skegg
  • add subtract, based on ASCII character codes: if A contains 'B', then by subtracting 'A' we get 1, and then adding 'a' we get 'b'. Well, it is generally on extreme, if I forgot what is where. And in the case of other layouts (for example, Cyrillic), problems will arise, since their codes do not go under the row. - Kozlov Sergei
  • And if it lies in the character 'a', i.e. lowercase character, what happens as a result? - skegg 2:21
  • Well, for this, I always perform the check 'A' <= A <= 'Z'))) but it will be a misfortune, we will get something in A, but definitely not 'A'! - Kozlov Sergei
  • Yeah, without this nowhere. But it is better to use a standard function that will do the same. At least save time. - skegg

There is a tolower function that converts characters to lower case. You can apply it to all characters in the string:

 #include <algorithm> #include <сctype> #include <iostream> #include <string> int main() { std::string s = "IaFFSjndsUFfE"; std::transform(s.begin(), s.end(), s.begin(), tolower); std::cout << s << std::endl; } 
  • Graceful and elegant. By the way, for C ++ there is a special recommended cctype header - skegg
  • @skegg changed, thanks! - dzhioev 8:38 pm
  • Nice, but it calls UB on negative character codes. std::transform(..., [](unsigned char c){return std::tolower(c)}); . - HolyBlackCat

Hardly toLower works with Russian letters.

The first way is to create an array and transform characters according to it:

 char str[]="СтрОка, ПЕРЕвоДимая, В НИЖНИЙ РЕГИСТР."; char[256] table={/*Эту таблицу надо заполнить вручную*/}; for(int i=0; i<sizeof(str)-1; i++) str[i]=table[str[i]]; 

The second way is to use the difference between small and large characters:

 for(int i=0; i<sizeof(str)-1; i++) { if(str[i]>'A' && str[i]<'Z') str[i]+='z'-'Z'; if(str[i]>'А' && str[i]<'Я') str[i]+='я'-'Я'; } 

The first method works faster if the table is pre-filled.

  • 2
    As for tolower and Russian letters, it was defeated on the line. Indeed, it does not work. To convert normally, you need to create the string wchar_t and then work with it on the towlower function from wchar.h - skegg

You can start by using the tolower () function from ctype.h