繁体   English   中英

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

[英]Tolower function for array of strings in C

我有一个字符串数组,我正在尝试将所有字符都转换为小写。

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

我知道tolower函数一次读取一个字符,而不是一次读取整个字符串。 这就是为什么我认为我必须使用这样的循环,但是仍然收到警告并且该功能不起作用的原因:

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]

我将衷心感谢您的帮助。

您需要一对嵌套循环,一个嵌套字符串,一个嵌套字符串。

#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

暂无
暂无

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

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