kk, if this is C then you use a test against '\0' to look for the end and do sumthin' like this.
int i;
for (i = 0; str[i] != '\0'; i++)
str[i] = (char)tolower(str[i]);
One of the few things a cutie like me is anal about is avoiding pointers when they aren't needed. Employers like to think that this is a better solution. I haven't figured out why yet.
char *p;
for (p = str; *p != '\0'; p++)
*p = (char)tolower(*p);
In fact, at my last interview, the dude's insistence on using pointers for something stupid was the deal breaker for an otherwise sweet gig. That's how silly I am about it.
But! Back to the present, the first loopy makes it clear that you're working with a string. Not just a string, a string that lives in an array. In other words, you can
change it! If it's a pointer, you jus' can't be sure, can ya? Sure, sure, if it's a function parameter then the point is moot because you've got a pointer whether you like it or not. But at the very least, the array notation is a cool little mnemonic for those of us that just aren't that bright. Words of wisdom (ha!) from a grunt in the trenches, yo.
void StrLower(char str[])
{
int i;
for (i = 0; str[i] != '\0'; i++)
str[i] = (char)tolower(str[i]);
}
If this is C++ then you've got options. And do I mean options! It's not C++ if you don't complicate the world, right?
struct Lowey {
int operator()(int c)
{
return std::tolower(c);
}
};
std::transform(str.begin(), str.end(), str.begin(), Lowey());
transform comes from the algorithm header, and tolower is in cctype. You can also use tolower directly, but it's troublesome.
std::transform(str.begin(), str.end(), str.begin(), std::tolower);
The biggest trouble is that both iostream and cctype declare a tolower, and there'd be an ambigimuity. :p But people look at me funny if I don't abusively use the STL in my C++ codage. Hmm, then again, they may look at me funny because I do...