如何将字符串转换为小写?
How do I convert a string to lowercase?
我不知道如何在cs50中将单词转换为完整的小写。我必须将单词转换为小写才能正确检查。
以下是我目前的代码
bool check(const char *word) {
char *lword[strlen(word)];
for (i = 0; i < strlen(word); i++) {
lword[i] = tolower(int
word[i]);
}
node *current;
int hashnum = hash(word);
if (table[hashnum] == NULL)
return false;
current = table[hashnum];
while (current->next != NULL) {
if (strcmp(current->word, word) == 0)
return true;
else
current = current->next;
}
return false;
}
这个声明char *lword[strlen(word)];
有问题。它将 lword
声明为 strings(又名 char*)的数组。 chars 的数组会更合适。 (此外,当 lword
作为参数发送给 hash
函数时,程序可能会报错。)不要忘记声明小写单词足够大以容纳 null-terminator,并且 null-terminate它。不要忘记将 lower-case 单词发送到散列,而不是原始单词。
我不知道如何在cs50中将单词转换为完整的小写。我必须将单词转换为小写才能正确检查。 以下是我目前的代码
bool check(const char *word) {
char *lword[strlen(word)];
for (i = 0; i < strlen(word); i++) {
lword[i] = tolower(int
word[i]);
}
node *current;
int hashnum = hash(word);
if (table[hashnum] == NULL)
return false;
current = table[hashnum];
while (current->next != NULL) {
if (strcmp(current->word, word) == 0)
return true;
else
current = current->next;
}
return false;
}
这个声明char *lword[strlen(word)];
有问题。它将 lword
声明为 strings(又名 char*)的数组。 chars 的数组会更合适。 (此外,当 lword
作为参数发送给 hash
函数时,程序可能会报错。)不要忘记声明小写单词足够大以容纳 null-terminator,并且 null-terminate它。不要忘记将 lower-case 单词发送到散列,而不是原始单词。