C 转换和计数大小写字符

C 转换和计数大小写字符,c,C,我正在编写一个C程序,将文件中的所有大写字符转换为小写字符,并将所有小写字符转换为大写字符 我还想计算读取的字符数,以及转换为大写和小写的字符数 我能够转换字符,但无法计算它们的数量 示例 Hello World! hELLO wORLD! 输出 Hello World! hELLO wORLD! 总共读13个字符 8转换为大写 2已转换为小写 这是我的密码 #include <stdio.h> #include <stdlib.h> #include

我正在编写一个C程序,将文件中的所有大写字符转换为小写字符,并将所有小写字符转换为大写字符

我还想计算读取的字符数,以及转换为大写和小写的字符数

我能够转换字符,但无法计算它们的数量

示例

Hello World! 
hELLO wORLD! 
输出

Hello World! 
hELLO wORLD! 
  • 总共读13个字符
  • 8转换为大写
  • 2已转换为小写
这是我的密码

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

#define INPUT_FILE      "input.txt"
#define OUTPUT_FILE     "output.txt"

int main()
{
    FILE *inputFile = fopen(INPUT_FILE, "rt");
    if (NULL == inputFile) {
        printf("ERROR: cannot open the file: %s\n", INPUT_FILE);
        return -1;
    }

    // 2. Open another file
    FILE *outputFile = fopen(OUTPUT_FILE, "wt");
    if (NULL == inputFile) {
        printf("ERROR: cannot open the file: %s\n", OUTPUT_FILE);
        return -1;
    }
    int c;
    int ch;
    int upper = 0;
    int lower = 0;
    int count = 0;
    while (EOF != (c = fgetc(inputFile))) {
        ch = islower(c)? toupper(c) : tolower(c);
        fputc(ch, outputFile);
    }
    while (EOF != (c = fgetc(inputFile))) {
        if (isupper(c))
        {
            upper++;
        }


        else if (islower(c))
        {
            lower++;
        }
        fputc(upper, outputFile);
        fputc(lower, outputFile);
    }

    fclose(inputFile);
    fclose(outputFile);

    return 0;

}
#包括
#包括
#包括
#定义输入文件“INPUT.txt”
#定义输出文件“OUTPUT.txt”
int main()
{
FILE*inputFile=fopen(输入文件,“rt”);
if(NULL==inputFile){
printf(“错误:无法打开文件:%s\n”,输入\u文件);
返回-1;
}
//2.打开另一个文件
FILE*outputFile=fopen(输出文件,“wt”);
if(NULL==inputFile){
printf(“错误:无法打开文件:%s\n”,输出文件);
返回-1;
}
INTC;
int-ch;
整数上限=0;
整数下限=0;
整数计数=0;
而(EOF!=(c=fgetc(inputFile))){
ch=岛下(c)-岛上(c):岛下(c);
fputc(ch,输出文件);
}
而(EOF!=(c=fgetc(inputFile))){
如果(上(c))
{
上位机++;
}
如有其他情况(岛下(c))
{
低级++;
}
fputc(上限,输出文件);
fputc(较低,输出文件);
}
fclose(输入文件);
fclose(输出文件);
返回0;
}

您的主要问题是使用2个循环读取输入文件。 在开始重新读取文件之前,您的第二个循环应该先读取该文件

您可以使用单个循环进行计数和转换

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

#define INPUT_FILE      "input.txt"
#define OUTPUT_FILE     "output.txt"

int main()
{
    FILE *inputFile = fopen(INPUT_FILE, "rt");
    if (NULL == inputFile) {
        printf("ERROR: cannot open the file: %s\n", INPUT_FILE);
        return -1;
    }

    // 2. Open another file
    FILE *outputFile = fopen(OUTPUT_FILE, "w");
    if (NULL == outputFile) {
        printf("ERROR: cannot open the file: %s\n", OUTPUT_FILE);
        return -1;
    }

    int ch;
    int upper = 0;
    int lower = 0;
    int count = 0;

    while (EOF != (ch = fgetc(inputFile)))
    {
        if (isalpha(ch))
        {
            if (islower(ch))
            {
                ch = toupper(ch);
                upper++;

            }
            else
            {
                ch = tolower(ch);
                lower++;
            }

            count++;
        }

        fputc(ch, outputFile);
    }

    fprintf(outputFile, "\nTotal: %d\nToUpper: %d\nToLower: %d\n", count, upper, lower);

    fclose(inputFile);
    fclose(outputFile);

    return 0;
}
#包括
#包括
#包括
#定义输入文件“INPUT.txt”
#定义输出文件“OUTPUT.txt”
int main()
{
FILE*inputFile=fopen(输入文件,“rt”);
if(NULL==inputFile){
printf(“错误:无法打开文件:%s\n”,输入\u文件);
返回-1;
}
//2.打开另一个文件
FILE*outputFile=fopen(输出文件,“w”);
if(NULL==outputFile){
printf(“错误:无法打开文件:%s\n”,输出文件);
返回-1;
}
int-ch;
整数上限=0;
整数下限=0;
整数计数=0;
而(EOF!=(ch=fgetc(inputFile)))
{
if(艾萨帕(ch))
{
if(岛下(ch))
{
ch=toupper(ch);
上位机++;
}
其他的
{
ch=托洛尔(ch);
低级++;
}
计数++;
}
fputc(ch,输出文件);
}
fprintf(输出文件,“\n总计:%d\n上限:%d\n下限:%d\n”,计数,上限,下限);
fclose(输入文件);
fclose(输出文件);
返回0;
}

还需要注意的是,在转换大小写之前,您必须检查read char是否是alpha char,就像循环中的
isalpha
调用所做的那样。

Yoda条件不需要,编译器已经改进了它们。更不用说第二个
if
语句无论如何都是错误的,这证明了Yoda条件与人的出错能力不匹配。第二个
if(NULL==inputFile)
-->
if(NULL==outputFile)
我在输出文件中得到hELLO wORLD输出,但不是计数。第一个
while
循环读取整个文件。因此,第二个
while
循环读取
EOF
,并且从不运行。解决方案是在第一个
while
循环中计算大小写字母,然后删除第二个
while
循环。您永远不会进入第二个循环。为什么不在同一个循环中转换和计数?