I have an array of strings and I'm trying to convert all characters to lower case.
void make_lower(char **array)
{
int i = 0;
while (array[i] != NULL){
array[i] = tolower(array[i]);
i++;
}
}
I know that tolower function reads characters one at a time, not the whole string at once. That's why I thought I had to use a loop like this, but still I get warnings and the function doesn't work:
passing argument 1 of ‘tolower’ makes integer from pointer without
a cast [-Werror]
note: expected ‘int’ but argument is of type ‘char *’
assignment makes pointer from integer without a cast [-Werror]
I would really appreciate your help.
You need a pair of nested loops, one for the string, one for the chars within it.
#include <stdio.h>
#include <ctype.h>
void make_lower(char **array)
{
int i = 0, j;
while (array[i] != NULL){
j = 0;
while (array[i][j] != '\0') {
array[i][j] = tolower(array[i][j]);
j++;
}
i++;
}
}
int main(void) {
char s1[]="ONE", s2[]="tWo", s3[]="thREE";
char *array[] = {s1, s2, s3, NULL };
make_lower(array);
printf ("%s\n", array[0]);
printf ("%s\n", array[1]);
printf ("%s\n", array[2]);
return 0;
}
Program outout:
one
two
three