change to lowercase character c++
To change a string to lowercase in C++, you can use the tolower
function from the <cctype>
header. Here are the steps:
- Include the
<cctype>
header at the beginning of your code:
#include <cctype>
- Declare a string variable and initialize it with the string you want to convert:
std::string str = "C++";
- Loop through each character in the string and convert it to lowercase using the
tolower
function:
for (char& c : str) {
c = std::tolower(c);
}
- Print the lowercase string:
std::cout << str << std::endl;
The tolower
function takes a character as input and returns the lowercase version of that character. In the loop, we're using a range-based for loop to iterate through each character in the string. The char& c
syntax allows us to modify each character in-place. By calling std::tolower(c)
, we're converting the character to lowercase. Finally, we print the modified string using std::cout
.
Here's the complete code:
#include <iostream>
#include <cctype>
int main() {
std::string str = "C++";
for (char& c : str) {
c = std::tolower(c);
}
std::cout << str << std::endl;
return 0;
}
When you run this code, it will output "c++", which is the lowercase version of the original string "C++".