DEV Community

Discussion on: Solution: To Lower Case

Collapse
 
seanpgallivan profile image
seanpgallivan

That misses the point of the exercise, though. Almost all languages have some kind of toLower method built-in. The exercise is to see if we can write our own code to do the same thing that the built-in methods do, to help us understand the process behind-the-scenes.

I mean, Javascript and Java have s.toLowerCase() and Python has s.lower(), all of which directly provide the solution.

Also, for C++, you can omit the explicit lambda from your example and just directly apply the standard function:

class Solution {
public:
    string toLowerCase(string s) {
        transform(s.begin(), s.end(), s.begin(), ::tolower);
        return s;
    }
];
Enter fullscreen mode Exit fullscreen mode

And there's also the simple iteration:

class Solution {
public:
    string toLowerCase(string s) {
        for (auto& c : s) c = tolower(c);
        return s;
    }
];
Enter fullscreen mode Exit fullscreen mode
Collapse
 
10xlearner profile image
10x learner

Indeed, now that you've pointed it out, with the purpose of understanding how tolower functions of programming languages works, your solutions are clearly more appropriate ! 👍

Nice point on the C++ example ! I always forget that we can pass functions like that, without the explicit lambda! 😄 👌

Maybe, it would have been interesting to mention the tolower of the language you used, directly in your article, for people to learn about them if they didn't.
In my little experience as a software engineer, people tends to re-write that kind of function each time they need it, even if they already exists and are available in their language, only because they don't know about them. I would be very interested to know if you had some similar experiences about that ? 😃