目前我有网站存储在char *禁止[100],我想将它们全部转换为小写 使用:
char *banned[100];
int x = 0;
if(fgets(temp, 100, file) != NULL) {
char *tempstore;
tempstore = (char*) malloc(sizeof(temp));
strcpy(tempstore, temp);
banned[x] = tempstore;
x++;
}
char temps[100];
while(banned[c]){
temps[c]=putchar(tolower(*banned[c]));
c++;
}
但结果并不是我所期待的。我可以得到一些关于我做错的提示/提示吗?
答案 0 :(得分:2)
由于你知道字符串(char数组)的大小,你可以使用for
循环。
char temps[100];
size_t i;
for(i = 0; i < 100; i++)
temps[i] = tolower(temps[i]);
答案 1 :(得分:1)
使用以下代码满足您的要求,
char *strtolower(char *s)
{
char *d = (char *)malloc(strlen(s));
while (*s)
{
*d =tolower(*s);
d++;
s++;
}
return d;
}
int main(void)
{
char *banned[100];
char *temps[100];
char temp[100];
FILE *file = fopen("test.txt", "r");
int x = 0;
if (file != NULL)
{
while(fgets(temp, 100, file) != NULL)
{
char *tempstore;
tempstore = (char *) malloc(sizeof(temp));
strcpy(tempstore, temp);
banned[x] = tempstore;
x++;
puts(tempstore);
}
int c = 0;
while(c < x)
{
temps[c] = strtolower(banned[c]);
puts(temps[c]);
c++;
}
}
return 0;
}
答案 2 :(得分:0)
您可以尝试以下方法:
char temps[100];
size_t i;
for(i = 0; temps[i] != '\0'; i++)
temps[i] = tolower(temps[i]);