📅  最后修改于: 2023-12-03 15:10:23.428000             🧑  作者: Mango
这是一个能将文件中的小写字母转换为大写字母的C程序。
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *fp;
char c;
fp = fopen("input.txt", "r");
if (fp == NULL) {
printf("Unable to open file.\n");
return 0;
}
while ((c = fgetc(fp)) != EOF) {
if (islower(c))
c = toupper(c);
printf("%c", c);
}
fclose(fp);
return 0;
}
该程序首先通过 fopen
函数打开名为 input.txt
的文件,如果打开失败则输出一条错误信息并返回 0。然后程序通过 fgetc
函数从文件中读取一个字符,如果读取到了文件结束符,则结束循环。在循环中,程序检查每个读取到的字符,如果该字符是小写字母,则将其转换为大写字母,并输出到标准输出流中。
最后,程序关闭文件并返回 0。
这是一个能将文件中的大写字母转换为小写字母的C程序。
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *fp;
char c;
fp = fopen("input.txt", "r");
if (fp == NULL) {
printf("Unable to open file.\n");
return 0;
}
while ((c = fgetc(fp)) != EOF) {
if (isupper(c))
c = tolower(c);
printf("%c", c);
}
fclose(fp);
return 0;
}
该程序与前一个程序基本相同,只是将检查字符是否为小写字母的语句改为检查字符是否为大写字母,并将转换函数从 toupper
改为 tolower
。