简体   繁体   English

用于C中字符串数组的Tolower函数

[英]Tolower function for array of strings in C

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. 我知道tolower函数一次读取一个字符,而不是一次读取整个字符串。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM