0

文字列の配列があり、すべての文字を小文字に変換しようとしています。

void make_lower(char **array)
{   
int i = 0;
while (array[i] != NULL){
       array[i] = tolower(array[i]);
       i++;
}
}

tolower 関数は文字列全体を一度に読み取るのではなく、文字を 1 つずつ読み取ることを知っています。そのため、このようなループを使用する必要があると考えましたが、それでも警告が表示され、関数は機能しません:

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]

よろしくお願いします。

4

1 に答える 1

4

ネストされたループのペアが必要です。1 つは文字列用、もう 1 つは文字列用です。

#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;
}

プログラム出力:

one
two
three
于 2015-04-15T07:47:50.370 に答える