04.编一个C程序,创建一个result.txt文件,从键盘上输入一个字符: 若该字符是数字字符,则把它转换为对应的整数并输出到result.txt中; 若该字符是大写字母,则转换成小写并输出到result.txt中; 若该字符是小写字母,则转换为大写并输出到result.txt中; 若该字符是其他字符,则不进行任何操作。
时间: 2023-05-11 18:03:51 浏览: 42
以下是编写的C程序,可以实现您的要求:
```c
#include <stdio.h>
int main() {
char c;
FILE *fp;
fp = fopen("result.txt", "w");
printf("请输入一个字符:");
scanf("%c", &c);
if (c >= '0' && c <= '9') {
fprintf(fp, "%d", c - '0');
} else if (c >= 'A' && c <= 'Z') {
fprintf(fp, "%c", c + 32);
} else if (c >= 'a' && c <= 'z') {
fprintf(fp, "%c", c - 32);
}
fclose(fp);
return 0;
}
```
这个程序会从键盘上读入一个字符,然后根据字符的类型进行相应的转换,并将结果输出到result.txt文件中。如果输入的字符不是数字字符、大写字母或小写字母,则不进行任何操作。
相关问题
编一个C语言程序,创建一个result.txt文件,从键盘上输入一个字符: 若该字符是数字字符,则把它转换为对应的整数并输出到result.txt中; 若该字符是大写字母,则转换成小写并输出到result.txt中; 若该字符是小写字母,则转换为大写并输出到result.txt中; 若该字符是其他字符,则不进行任何操作。
以下是编写的C语言程序,实现了上述功能:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char c;
FILE *fp;
fp = fopen("result.txt", "w");
printf("请输入一个字符:");
scanf("%c", &c);
if (isdigit(c)) {
fprintf(fp, "%d", c - '0');
} else if (isupper(c)) {
fprintf(fp, "%c", tolower(c));
} else if (islower(c)) {
fprintf(fp, "%c", toupper(c));
}
fclose(fp);
return 0;
}
```
这个程序会在当前目录下创建一个result.txt文件,并从键盘上读取一个字符,根据字符的类型进行相应的转换并输出到result.txt中。如果输入的字符是其他字符,则不进行任何操作。
用C语言写一个程序,功能是从键盘上依次输入一个整数1234、一个字符a、一个实数5.6,用fprintf函数写入文件result.txt中。
以下是用C语言编写的程序,可以实现从键盘上依次输入一个整数1234、一个字符a、一个实数5.6,并将它们写入文件result.txt中:
```c
#include <stdio.h>
int main() {
int num = 1234;
char ch = 'a';
float fnum = 5.6;
FILE *fp = fopen("result.txt", "w");
if (fp == NULL) {
printf("Failed to open file.\n");
return 1;
}
fprintf(fp, "%d %c %.1f", num, ch, fnum);
fclose(fp);
return 0;
}
```
这个程序中,我们首先定义了一个整数、一个字符和一个实数,并将它们赋值为1234、'a'和5.6。然后,我们使用fopen函数打开文件result.txt,并将文件指针保存在变量fp中。如果打开文件失败,我们会输出一条错误信息并返回1。接着,我们使用fprintf函数将整数、字符和实数写入文件result.txt中。最后,我们使用fclose函数关闭文件。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)