Conert a string to lowercase in c++

文章目录

  • Using ranged for loop
  • using std::for_each
  • using std::transform

Using ranged for loop

char toLowerCase(char c)
{
	if (c >= 'A' && c <= 'Z') {
		return c + 32;
	}

	return c;
}
std::string str{"ABC"};
for (auto& c : str) {
	c = toLowerCase(c);
}

using std::for_each

std::string str{"ABC"};
std::for_each(str.begin(), str.end(), [](char& c) {
		c = ::to_lower(static_cast<unsigned char>(c));
	});

using std::transform

std::string str{"ABC"};
std::transform(str.begin(), str.end(), str.begin(), ::tolower());

你可能感兴趣的:(c/c++)