Question: C++ string to lowercase! How do I define a function that takes a reference to aC++ style string and modify that string so that all

C++ string to lowercase!

How do I define a function that takes a reference to aC++ style string and modify that string so that all of thealphabetic characters in it become lowercase (hints: variables oftype char can be treated as integers that can be added andsubtracted, the range of values for 'a' through 'z' is contiguous,and the range of values for 'A' through 'Z' is alsocontiguous).

I cannot use the built in tolower() function in c++, Ihave to write my own. My problem is, if I write this function thattakes in a char * str, I have all the characters inside a chararray. I can loop through the array but I cannot modify the char atany index of the array because c++ won't allow it. Why is that? Isthere another way I can do this without using tolower()? I have toreturn that same string but all in lowercase.

Here's the code I already have:

char * lowercase(char * str) {

char * ptr;
for (ptr = str; *ptr != '0'; ptr++) {
char boo = *ptr;

if (boo <= 'Z'&& boo >= 'A') {
boo = boo- ('Z' - 'z');
*ptr =boo; // this line is not working
}
}
return str;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!