C //习题10.3 从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“test“中保存,输入的字符串以“!“结束。

C程序设计 (第四版) 谭浩强 习题10.3

习题10.3 从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件"test"中保存,输入的字符串以"!"结束。

IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。
代码块
方法:使用指针,函数的模块化设计,动态分配内存
说明:这里准备先写入的文件名称为Test.txt,文件已经存在于该项目目录下。
c 复制代码
#include <stdio.h>
#include <stdlib.h>

void initialVar(char **name, char **word){
	*name = (char*)malloc(80 * sizeof(char));
	*word = (char*)malloc(sizeof(char));
}

void inputFileName(FILE **file, char *name){
	printf("Enter File Name: ");
	scanf("%s", name);

	*file = fopen(name, "r");
	if(*file == NULL){
		perror("Cannot open this file");
		system("pause");
		exit(0);
	}
}

void fileInput(FILE **file, char *name, char *word){
	*file = fopen(name, "w+");
	if(*file == NULL){
		perror("Cannot open this file");
		system("pause");
		exit(0);
	}

	printf("Enter String: ");
	while((*word = getchar()) != '!'){
		if(*word >= 'a' && *word <= 'z'){
			*word -= 32;
		}
		fputc(*word, *file);
	}
	fclose(*file);
	putchar(10);
}

void freeVar(char **name, char **word){
	free(*name);
	free(*word);
}

int main(){
	FILE *file = NULL;
	char *name = NULL;
	char *word = NULL;

	initialVar(&name, &word);
	inputFileName(&file, name);
	fileInput(&file, name, word);
	freeVar(&name, &word);

	system("pause");
	return 0;
}
运行结果如下:
相关推荐
Alicx.4 分钟前
dfs由易到难
算法·蓝桥杯·宽度优先
_日拱一卒16 分钟前
LeetCode:找到字符串中的所有字母异位词
算法·leetcode
云泽8081 小时前
深入 AVL 树:原理剖析、旋转算法与性能评估
数据结构·c++·算法
Wilber的技术分享2 小时前
【LeetCode高频手撕题 2】面试中常见的手撕算法题(小红书)
笔记·算法·leetcode·面试
邪神与厨二病2 小时前
Problem L. ZZUPC
c++·数学·算法·前缀和
梯度下降中3 小时前
LoRA原理精讲
人工智能·算法·机器学习
IronMurphy3 小时前
【算法三十一】46. 全排列
算法·leetcode·职场和发展
czlczl200209253 小时前
力扣1911. 最大交替子序列和
算法·leetcode·动态规划
靴子学长4 小时前
Decoder only 架构下 - KV cache 的理解
pytorch·深度学习·算法·大模型·kv
寒秋花开曾相惜4 小时前
(学习笔记)3.8 指针运算(3.8.3 嵌套的数组& 3.8.4 定长数组)
java·开发语言·笔记·学习·算法