我有一个包含一些单词的 .txt 文件,我需要它们是小写的。如何使每个单词小写?仅将 tolower() 添加到 strtok() 是行不通的。我应该添加什么?或者也许首先在整个文件上使用 tolower() 会更容易?但是怎么做?请帮忙!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <ctype.h>
int main(void)
{
char str[5000];
char *ptr;
char *words[5000];
FILE * fp = fopen("hi.txt", "r");
fgets(str, 49, fp);
ptr = strtok(str, ",.; ");
int i = 0;
while(ptr != NULL)
{
words[i]= ptr;
i++;
ptr = strtok(NULL, ",.; ");
}
fclose(fp);
for(int j=0;j<i;j++) {
printf("%s\n", words[j]);
//printf("%s\n", tolower(words[j])); // Doesn't work!
}
return 0;
}
例子:
嗨.txt
Foo;
Bar.
Baz.
预期产出
foo
bar
baz