In this article, we will learn 3 different ways to convert a string to lower case or small letters in C++ with example programs.
Difference between upper and lower case letters in ASCII table is 32. Example, ASCII code for A is 65 and a is 97; B is 66 and b is 98.
So, if we add 32 to a character, it will be converted to its lower case.
Based on this reasoning, this method iterates through the characters of the string, adds 32 to each character and assigns it back to its position in the string.
Example program based on this method is given below.
#include <iostream> #include <string> using namespace std; int main() { string s = "HELLO"; cout << "String in upper case: " << s << endl; for(int i=0; i < s.length(); i++){ s[i] = s[i] + 32; } cout << "String in lower case: " << s << endl; }
which outputs
String in upper case: HELLO
String in lower case: hello
Note that the loop iterates from 0 till one less than the length of the string. This is because characters in a string are indexed from 0.
transform()
function is used to apply an operation over an array or a structure which can be iterated such as a string. It takes 4 arguments:
1. Iterator at the beginning of the range on which you want to apply the operation. In this case, it will be the start of string.
2. Iterator at the end of the range on which you want to apply the operation. In this case, it will be the end of string.
3. Iterator at the beginning of the range on which you want the result of operation to be stored. In this case, it will be the start of string.
4. Operation that needs to be applied. In this example, it will be tolower
.
Example program is given below.
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string s = "HELLO"; cout << "String in upper case: " << s << endl; transform(s.begin(), s.end(),s.begin(), ::tolower); cout << "String in lower case: " << s << endl; }
Iterators at the beginning and end of the string can be obtained using its begin()
and end()
functions respectively.
Since we need to apply tolower
operation on the same string, we supply the iterator positioned at its beginning as the third argument.
transform()
function is defined in algorithm
header file, so it needs to be included.
Output is
String in upper case: HELLO
String in lower case: hello
This method is based on the following steps.
1. Initialize an empty string to hold the string converted in lower case.
2. Iterate over the string in upper case using a for loop.
3. In every iteration, convert the current character to lower case using
tolower()
function and assign it to the corresponding index of the string created in step 1. After the loop completes, the string initialized in step 1 will have the source string in lower case.
Example program follows.
#include <iostream> #include <string> using namespace std; int main() { string cap = "HELLO"; string lower = ""; lower.resize(cap.length()); // iterate over string for (int i = 0; i < cap.length(); i++) { // convert character to lower case lower[i] = tolower(cap[i]); } cout << "String in upper case: " << s << endl; cout << "String in lower case: " << s << endl; }
Output is
String in upper case: HELLO
String in lower case: hello
This method does not modify the original string as the earlier two methods.