Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

C++ tolower() Function

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Hey there, fellow coder! Ready to dive into the exciting world of C++ character functions? In this article, we're going to break down the usage of the tolower() function in C++. This handy function comes into play when you're dealing with case-sensitive data and you need to transform uppercase characters to lowercase.

C++ tolower() function

In this blog, we will learn about C++ tolower() function in detail. 

Unraveling the tolower() Function

In C++, the tolower() function is a built-in function declared in the <cctype> library. It's used to convert a given character to lowercase. If the input character is already lowercase or isn't an alphabetic character, the function simply returns the original character.

Basic Usage of tolower()

The tolower() function takes a single argument, which is the character you want to convert. Here's a simple example:

#include <cctype>
#include <iostream>


int main() {
    char upper = 'A';
    char lower = tolower(upper);


    std::cout << lower;
    return 0;
}

When you run this program, it will output 'a'.

Output

Output

Using tolower() in a String

The power of the tolower() function really shines when working with strings. Suppose you have a string of text and you want to convert it all to lowercase. Here's how you can do it:

#include <cctype>
#include <iostream>
#include <string>


int main() {
    std::string text = "Hello, World!";
    for (int i = 0; i < text.size(); i++) {
        text[i] = tolower(text[i]);
    }

    std::cout << text;
    return 0;
}

Output

Output

In this example, the program will output 'hello, world!'.

Exceptions

It's important to note that tolower() only accepts an int type as an argument, not a char. However, in C++, a char is implicitly convertible to an int, which is why it can be used directly. If you try to pass a non-integer type that can't be converted to an integer, your compiler will throw an error.

Also see, Abstract Data Types in C++ File Handling in CPP

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What is the purpose of the tolower() function in C++?

The tolower() function is used to convert a given character to its lowercase equivalent in C++.

Can tolower() convert non-alphabetic characters?

No, tolower() only affects uppercase alphabetic characters. Non-alphabetic characters are returned unchanged.

Which library is needed for using tolower() in C++?

The tolower() function is included in the <cctype> library in C++.

Conclusion

The tolower() function in C++ is an easy-to-use and powerful tool for managing case sensitivity in your programs. With this function, you can ensure that user input or data processing is handled consistently, regardless of the case used in the source data. So, the next time you need to convert a string to lowercase, remember tolower() is your friend. 

Recommended articles:


We hope this blog helped you to understand the concept of the Member Function in C++. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Happy Learning!! 

Topics covered
1.
Introduction
2.
Unraveling the tolower() Function
2.1.
Basic Usage of tolower()
2.2.
Using tolower() in a String
2.3.
Exceptions
3.
Frequently Asked Questions
3.1.
What is the purpose of the tolower() function in C++?
3.2.
Can tolower() convert non-alphabetic characters?
3.3.
Which library is needed for using tolower() in C++?
4.
Conclusion