1

I am trying to use the replace function to replace uppercase letters with lowercase ones. The way I was trying to do it was that when the letter being checked is between ASCII values 65-90, it would be replaced with the character that is 32 ASCII values higher, for example, 65 would be turned to 97 (A to a).

char holder = (userPhrase[count]) + 32;
jumbledPhrase.replace(count, 1, holder);

The point of the variable holder to hold the new value to be used in replace, I am getting an error saying holder does not count as a char parameter in the replace function. Can replace even be used in the way I am trying to use it?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
The_Redhawk
  • 214
  • 1
  • 2
  • 11
  • 1
    Unrelated: Is the `tolower` function forbidden for this assignment? Using the library functions is almost always better because the writers have taken into account many edgecases you may have missed, such as a non-ASCII target, and their assumptions will have held true for millions upon millions of C++ programs. – user4581301 Nov 02 '20 at 19:42
  • 2
    If you get a compiler error please post the error in the question and show a [mre]. Related question: https://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case – Lukas-T Nov 02 '20 at 19:44
  • @user4581301 I'm sure it's deliberately ironic that `std::tolower` is completely inappropriate for any multi- or variable-byte length encoding (e.g. UTF-8) and is really only useful for the ancient 8-bit "extended ASCII" codepages. Unless you have tight, controlled input constraints, you should not be writing `std::tolower` in _one_ C++ program, let alone millions upon millions. – Asteroids With Wings Nov 02 '20 at 20:41
  • There is the [locale-aware version](https://en.cppreference.com/w/cpp/locale/tolower), but this is more a general problem with C++'s confused handling of wide characters. My point is `tolower` has been deployed countless times over decades with a far higher success rate than the nigh-infinite number of roll-your-own versions out there. – user4581301 Nov 02 '20 at 20:58

3 Answers3

6

Use tolower() and make your (but also your code readers) life easier, by avoiding using magic numbers.

Read other alternatives in How to convert std::string to lower case?, as @churill commented, such as std::transform.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Related: [What is a magic number, and why is it bad?](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) – user4581301 Nov 02 '20 at 19:46
  • 1
    Good point. I will use that instead. Thanks. I will also look into transform to see how that works. For now john below gave a solution that is working. – The_Redhawk Nov 02 '20 at 20:30
  • @The_Redhawk glad I helped. All three answers work, just make sure you choose the right one for your needs, cheers. – gsamaras Nov 02 '20 at 22:46
1

I think you are looking for

jumbledPhrase.replace(count, 1, 1, holder);

which is replace 1 (the first 1) character at position count in string jumbledPhrase with 1 copy (the second 1) of the character holder.

Reference here, if actually you were looking for something else

john
  • 85,011
  • 4
  • 57
  • 81
  • Of course the more straightforward way to do this is `jumbledPhrase[count] = holder;` – john Nov 02 '20 at 21:10
1

Using replace() to replace a single char is overkill, just use operator[] instead, which you are already using to access the source char that is being replaced:

char holder = userPhrase[count] + 32;
jumbledPhrase[count] = holder;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770