I need to be able to convert case in strings, so that "Hello" becomes "hello". Ultimately, I need to compare them.
How would I convert:
char *string = "HEllO";
to the string "hello"?
compareToIgnoreCase method? You can do that by converting the letters to lower case as you compare them and not saving the entire string - twain249 2012-04-04 23:55
First, your string is not declared properly, it should be:
char *string = "Hello";
Now, to convert to lowercase, try this:
char *lowercase(const char *str)
{
int length = strlen(str);
char *dup = malloc(length + 1);
strcpy(dup, str);
for (int i = 0; i < length; i++)
{
dup[i] = tolower(dup[i]);
}
return dup; // remember to free when done!
}
"HEllO" is a valid string - twain249 2012-04-04 23:54
Simply perform this inline.
char string[] = "Hello";
char * ptr;
for (ptr = string; *ptr != '\0'; ++ptr)
{
*ptr = tolower(*ptr);
}
char string[] = ... would work - kay 2012-04-04 23:50