小写到大写
给定一个文本文件(gfg.txt),我们的任务是文件的小写字符转换为全部大写。
例子:
Input: (content inside file (gfg.txt)
Geeks Classes:
An extensive classroom programme
by GeeksforGeeks to build and enhance
Data Structures and Algorithm concepts
Output: (content inside file (gfg.txt)
GEEKS CLASSES:
AN EXTENSIVE CLASSROOM PROGRAMME
BY GEEKSFORGEEKS TO BUILD AND ENHANCE
DATA STRUCTURES AND ALGORITHM CONCEPTS
方法 :
在读取模式下打开文件gfg.txt。检查打开或查找文件时是否有任何错误。如果是,则抛出错误消息。
如果找到了文件,则在while循环的帮助下,使用该文件的toupper将所有字符转换为大写。
使用fclose()函数通过在文件中传递文件指针来关闭文件。
// C++ program to convert
// all lower case characters of a file
// into Upper Case
#include
int main()
{
// initializing the file pointer
FILE* fptr;
// name of the file as sample.txt
char file[50] = { "gfg.txt" };
char ch;
// opening the file in read mode
fptr = fopen(file, "r");
ch = fgetc(fptr);
// converting into upper case
while (ch != EOF) {
// converting char to upper case
ch = toupper(ch);
printf("%c", ch);
ch = fgetc(fptr);
}
// closing the file
fclose(fptr);
return 0;
}
输出:
GEEKS CLASSES:
AN EXTENSIVE CLASSROOM PROGRAMME
BY GEEKSFORGEEKS TO BUILD AND ENHANCE
DATA STRUCTURES AND ALGORITHM CONCEPTS
大写到小写:
与上述类似,仅使用下调函数代替上调
例子:
Input: (content inside file (gfg.txt)
Geeks Classes:
AN EXTENSIVE CLASSROOM PROGRAMME
BY GEEKSFORGEEKS TO BUILD AND ENHANCE
DATA STRUCTURES AND ALGORITHM CONCEPTS
Output: (content inside file (gfg.txt)
geeks classes:
an extensive classroom programme
by geeksforgeeks to build and enhance
data structures and algorithm concepts
// C++ program to convert all upper
// case characters of a file
// into lower Case
#include
int main()
{
// initializing the file pointer
FILE* fptr;
// name of the file as gfg.txt
char file[30] = { "gfg.txt" };
char ch;
// opening the file in read mode
fptr = fopen(file, "r");
ch = fgetc(fptr);
// converting into lower case
while (ch != EOF) {
// converting char to lower case
ch = tolower(ch);
printf("%c", ch);
ch = fgetc(fptr);
}
// closing the file
fclose(fptr);
return 0;
}
输出:
geeks classes:
an extensive classroom programme
by geeksforgeeks to build and enhance
data structures and algorithm concepts
笔记:
1. Run this program offline by making the file gfg.txt and store some characters in it.
2. Make sure that you have made the file with the same name as that used in code and within the same folder where your program is stored.
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。