有人能帮我解决这个代码吗?该函数需要像主题中编写的那样执行。可以吗?我更需要的是计算所做更改的数量。如何实现这一点?
int change(char *path){
FILE *f = fopen(path, "r");
if(f==NULL){
printf("Error...");
return -1;
}
int text, length;
fscanf(f, "%s", &text);
length = strlen(text);
for(i = 0; i < length; ++i){
if(islower(text[i]))
{
text[i] = toupper(text[i]);
}
if(isupper(text[i]))
{
text[i] = toslower(text[i]);
}
fprintf(f,"%s",text);
fclose(f);
最佳答案
现在,您的代码将首先尝试将文本从小写更改为大写,如果成功,则将其更改回小写。我认为这不是你想要的,因为你现在有两种情况,要么从下到上再回到下,要么根本不改变。
为了跟踪更改,我们添加了一个变量“更改”,并将其初始化为零。
相反,如果您想将字符更改为大写(如果是小写),如果是大写则更改为小写,请像这样重写:
if(islower(text[i])) {
text[i] = toupper(text[i]);
changes++;
} else if(isupper(text[i])) {
text[i] = tolower(text[i]);
changes++;
}
还有一个拼写错误,toslower(text[i]) 但我假设你的意思是 tolower(text[i])
关于c - C 中小写到大写和大写到小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13556561/