The_Redhawk
The_Redhawk

Reputation: 273

C++ replace function

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?

Upvotes: 1

Views: 224

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 596256

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;

Upvotes: 1

john
john

Reputation: 87959

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

Upvotes: 1

gsamaras
gsamaras

Reputation: 73366

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.

Upvotes: 6

Related Questions