Hi everybody,
I have an array of chars that I want to make all lower
int Scan(char Search[])
{
char *cPtr;
cPtr = strtok (Search," -,.");
while (cPtr != NULL)
{
// Here I want to lower cPtr whith somithing like cPtr =
tolower(cPtr)
//but don't works
printf("%s ", cPtr);
cPtr = strtok (NULL, " -,.");
}
}
strtok() is a subtly complicated function to use. Just loop through
your array testing for an alphabetic character with isalpha() and if
so, check if it's already in lowercase with islower() and if not,
convert with tolower().
The use of isalpha() and islower() functions is completely unnecessary
in this case.
islower() alone only returns non-zero if its argument is a lower case
alpha character.
But neither function is required. tolower(), for a valid argument,
returns a different value from the argument if and only if the
argument is an upper case alpha character with a lower case
equivalent, otherwise it returns its argument unchanged.
Note that the argument must be valid, however, namely an int with a
value of either EOF or in the range of 0 to UCHAR_MAX inclusive. On
implementations where char is signed, some characters might have
negative values and passing one of those to any of the to... or is...
functions produces undefined behavior.
The usual advice given here is to cast each character to unsigned char
when passing to the to... or is... functions. I don't particularly
like that approach, because some characters with negative values (such
as PC text mode "extended ASCII") will get changed that you probably
don't want changed.
The safest way to do this to only valid uppercase letters is something
like this (uncompiled and untested):
#include <ctype.h>
char *(string_to_lower char *s)
{
char *ret = s;
if (s)
{
while (*s)
{
if (*s > 0)
{
*s = tolower(*s);
}
++s;
}
}
return ret;
}