transform(s.begin(),s.end(),s.begin(),::tolower);
This is actually wrong. ::tolower
is deceptively difficult to use. The problem is:
- The parameter of
::tolower
is int.
- The behaviour is undefined if the argument is not representable as unsigned char.
- char may be a signed type, and thus its value isn't necessarily representable as unsigned char. Passing such char value would result in undefined behaviour.
The solution is to convert the char typed character into unsigned char before passing into ::tolower
. Example:
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
return ::tolower(c);
);
Note that ::tolower
processes one code unit at a time, and thus cannot work correctly with variable width character encodings such as Unicode.
The other, more obvious bug is:
string st;
That's an empty string.
st[j++]='.';
Here, you access the empty string outside of its bounds. The behaviour of the program is undefined.
A solution is to instead use:
st += '.';