1

I want to replace all of the Uppercase letters in string with lowercase letters is there any function to do so because the replace function is not giving any result. Here is my code..

 for (int i = 0 ; i < string. size() ; i++ ) { if (string[i] >= 65 && string[i] <= 90) { string.replace(string[i] , 1 ,string[i]+32); } } 
3

2 Answers 2

3

You should read the documentation for replace - first argument is position of the substring to replace, not the character you want to replace.

So, string.replace(i , 1 ,string[i]+32); should work. Personally, I would go with

for(auto& p : string) p=std::tolower(p); 
Sign up to request clarification or add additional context in comments.

2 Comments

You forgot to assign the tolower result.
@DanM. Ups, nice catch.
3

Another option is to use transform

#include <algorithm> #include <locale> int main() { std::transform(string.begin(), string.end(), string.begin(), ::tolower); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.