1

有人会帮我处理这段代码吗?编写主题中需要做的函数。可以吗?我还需要计算所做更改的数量。如何实现这一点?

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);
4

2 回答 2

1

现在,您的代码将首先尝试将文本从小写更改为大写,然后如果成功将其更改回小写。我认为这不是您想要的,因为您现在有两个案例,要么从低到高再回到低,要么根本没有变化。

为了跟踪更改,我们添加了一个变量“更改”,我们将其初始化为零。

相反,如果您想将字符更改为大写,如果它是小写,如果它是大写则改写为小写,如下所示:

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])

于 2012-11-25T23:18:33.970 回答
1

要计算更改的数量,只需创建一个变量 ( int count = 0) 并随着每次更改 ( count++) 递增它。

int change(char *path){


    FILE *f = fopen(path, "r");

    if(f==NULL){
        printf("Error...");
        return -1;
    }

    int text, length;
    int count = 0;

    fscanf(f, "%s", &text);
    length = strlen(text);

    for(i = 0; i < length; ++i){
        if(islower(text[i]))
        {
            text[i] = toupper(text[i]);
            count++;
        }
        if(isupper(text[i]))
        {
            text[i] = tolower(text[i]);
            count++;

        }
    }

    fprintf(f,"%s",text);
    fclose(f);
 }
于 2012-11-25T23:12:31.980 回答