gpt4 book ai didi

c - C 中的 tolower 函数不允许我转换字符串

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:38 26 4
gpt4 key购买 nike

考试的课文说

The program should execute in a case-insensitive way, e.g. "How" and "hoW" are the same word

所以我创建了一个字符,使用 atoi 将其传递给一个值,然后我尝试将其所有内容转换为小写。

示例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define STR_LEN 20

int main(int argc, char *argv[])
{

if(argc != 3) {
fprintf(stderr, "Incorrect number of arguments.\n");
exit(EXIT_FAILURE);
}

char word1[STR_LEN +1];
strcpy(word1, argv[1]);

int i;
for(i=0;i<strlen(word1);i++){
word1 = tolower(word1[i]);
}

char word2[STR_LEN +1];
strcpy(word2, argv[2]);

FILE *fin;
fin = fopen("MyTEXTFile.TXT", "r");
if(fin == NULL) {
fprintf(stderr, "Can't open the file.\n");
exit(EXIT_FAILURE);
}

char w1[STR_LEN +1] = "";
char w2[STR_LEN +1];
int found = 0;

while(fscanf(fin, "%s", w2) != EOF) {
printf("DEBUG: Checking \"%s\" \"%s\"\n", w1, w2);

if(strcmp(w1, word1)==0 && strcmp(w2, word2)==0) {
++found;
}
if(strcmp(w1, word2)==0 && strcmp(w2, word1)==0) {
++found;
}

strcpy(w1, w2);
}
fclose(fin);

if(found > 0) {
printf("The words \"%s\" and \"%s\" appear consecutively in the text %d times", word1, word2, found);
} else {
printf("The words \"%s\" and \"%s\" never appear consecutively in the text", word1, word2);
}

return EXIT_SUCCESS;
}

为什么程序在这里给我报错:

 int i;
for(i=0;i<strlen(word1);i++){
word1 = tolower(word1[i]);
}

将它们全部转换为小写的正确做法是什么?

最佳答案

更改为:

int i;
for(i=0;i<strlen(word1);i++){
word1[i] = tolower(word1[i]);
}

在你的代码中,你将word1的字符赋给字符串指针,非常糟糕

关于c - C 中的 tolower 函数不允许我转换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32170385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com