[英]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.