c - C 中小写到大写和大写到小写

标签 c uppercase lowercase

有人能帮我解决这个代码吗?该函数需要像主题中编写的那样执行。可以吗?我更需要的是计算所做更改的数量。如何实现这一点?

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/

相关文章:

fonts - SwiftUI 字体如何使用 uppercased() 和 LocalizedStringKey

查询中的 Grails lower() 不起作用

c - HTTP PUT 方法在 C 中的实现

c - C语言串口: read non-canonical mode

sql-server - 如何在 T-SQL 中重新创建 InitCap() 函数以将首字母大写应用于字符串?

regex - Notepad++ 和正则表达式 : how to UPPERCASE specific part of a string/find/replace

python - 如何从字符串中提取所有 UPPER? Python

Android ActionBar MenuItem 小写

c - 如何在 GTK 中进行简单的图形绘制?

c - 使用 C 中的函数打开套接字