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);
        }
    }
Ayoub
  • 17
  • 6
  • What is `string` here? Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Note that the 1st argument of [`std::string::replace`](https://en.cppreference.com/w/cpp/string/basic_string/replace) should be the index, not the character. – MikeCAT Mar 30 '21 at 11:53
  • see http://www.cplusplus.com/reference/locale/tolower/ – alon Mar 30 '21 at 11:54
  • Are you looking for http://www.cplusplus.com/reference/cctype/tolower/ – saumitra mallick Mar 30 '21 at 11:57

2 Answers2

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);
Quimby
  • 17,735
  • 4
  • 35
  • 55
3

Another option is to use transform

#include <algorithm>
#include <locale>

int main()
{
  std::transform(string.begin(), string.end(), string.begin(), ::tolower);
}
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98