6

Convert a character to lowercase in C++

 2 years ago
source link: https://thispointer.com/convert-a-character-to-lowercase-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

This article will discuss different ways to convert a character to lowercase in C++.

Convert a character to lowercase using tolower() function

C++ provides a function std::tolower(char c) to get a lowercase equivalent of a character. It accepts a character as an argument, and if this character is uppercase, it returns an lowercase equivalent of that character. We can use this function to convert a character to lowercase in C++. For example,

#include <iostream>
int main()
char ch = 'B';
// Convert a character to lowercase
ch = std::tolower(ch);
std::cout << ch <<std::endl;
return 0;
#include <iostream>

int main()
{
    char ch = 'B';

    // Convert a character to lowercase
    ch = std::tolower(ch);

    std::cout << ch <<std::endl;

    return 0;
}

Output:

b

It converted the uppercase character ‘B’ to an lowercase character ‘b’. If the provided character is not uppercase, then std::tolower() will return the same character. For example,

#include <iostream>
int main()
char ch = '+';
// Convert a character to lowercase
ch = std::tolower(ch);
std::cout << ch <<std::endl;
return 0;
#include <iostream>

int main()
{
    char ch = '+';

    // Convert a character to lowercase
    ch = std::tolower(ch);

    std::cout << ch <<std::endl;

    return 0;
}

Output:

Advertisements

vid5e6258f9da92c874459691.jpg?cbuster=1600267117
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY0Nwp1Nmx0MCZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDMmMxQmMDM4NUYmMTM4N0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR2Vwn28yMwxyMwBDnHJioWUyMxY3Nl4jLwM4NwUhMTIjJTIjU2FzYXJcJTJGNTM3LwM2JzNmqXVcZD02MwI3ODtmMmJxNWM3JzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NDY3NTp5NDE0MmEzqWyxPVNyn2yhZG9TUGkurWVlNwIlNmt4MmQjYTQjMvZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZwo252ZXJ0LWEgY2uupzFwqGVlLXRiLWkiq2VlY2FmZS1cov1wJTJGJzZfo2F0U3RuqHVmPWZuoHNyJzVcZHNjPXBlZWJcZA==
+

The provided character is not a uppercase character; therefore tolower() returned the same character.

Convert a character to lowercase by using the ASCII values

Every character in C++ has an ASCII value associated with it. In C++, the ASCII values of lowercase characters (i.e. ‘A’ to ‘Z’) in C++ are 65 to 90. The ASCII values of uppercase characters (i.e. ‘a’ to ‘z’) in C++ are 97 to 122. So, to convert a uppercase character to lowercase, we can add 32 to the character(its ASCII value). When we add anything to a character, the character gets automatically converted to an int (i.e. the ascii value), and the value is added. We can use this logic to convert a character to lowercase.

The steps are as follows,

  • Check if the provided character contains an alphabet and a uppercase character.
  • If yes, then add 32 to the character.

For example,

#include <iostream>
int main()
char ch = 'B';
if( isalpha(ch) && std::isupper(ch) )
ch += 32;
std::cout << ch <<std::endl;
return 0;
#include <iostream>

int main()
{
    char ch = 'B';

    if( isalpha(ch) && std::isupper(ch) )
    {
        ch += 32;
    }
    std::cout << ch <<std::endl;

    return 0;
}

Output:

b

It converted the uppercase character ‘B’ to an lowercase character ‘b’. Let’s check a negative example and convert the character ‘+’ to lowercase.

For example,

#include <iostream>
int main()
char ch = '+';
if( isalpha(ch) && std::isupper(ch) )
ch += 32;
std::cout << ch <<std::endl;
return 0;
#include <iostream>

int main()
{
    char ch = '+';

    if( isalpha(ch) && std::isupper(ch) )
    {
        ch += 32;
    }
    std::cout << ch <<std::endl;

    return 0;
}

Output:

+

The provided character is not a uppercase character; therefore, our code didn’t change its value.

Summary:

We learned about two different ways to convert a character to lowercase in C++.

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK