C Program for Lower Case to Uppercase and vice-versa in a file
Lower Case To Upper Case
Given a text file(gfg.txt), our task is to convert all the lower case characters of file into upper case.
Examples:
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
Approach :
Open the file gfg.txt in read mode. Check if there is any error in opening or locating a file. If yes, then throw an error message.
If the file is found, then with the help of while loop, convert all the characters using toupper of that file into upper case.
Close the file using fclose() function by passing the file pointer in it.
// C++ program to convert // all lower case characters of a file // into Upper Case #include <bits/stdc++.h> 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; } |
Output:
GEEKS CLASSES: AN EXTENSIVE CLASSROOM PROGRAMME BY GEEKSFORGEEKS TO BUILD AND ENHANCE DATA STRUCTURES AND ALGORITHM CONCEPTS
Upper Case to Lower Case:
Similar as above only using tolower function in place of toupper
Examples:
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 <bits/stdc++.h> 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; } |
Output:
geeks classes: an extensive classroom programme by geeksforgeeks to build and enhance data structures and algorithm concepts
Note:
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.
Recommended Posts:
- Check whether the given character is in upper case, lower case or non alphabetic character
- C program to copy contents of one file to another file
- Menu-Driven program using Switch-case in C
- C program for file Transfer using UDP
- C program to delete a file
- C Program to print contents of file
- LEX program to add line numbers to a given file
- Lex program to search a word in a file
- C Program to find size of a File
- C Program to count the Number of Characters in a File
- C Program to count number of lines in a file
- C Program to merge contents of two files into a third file
- Write a C program that displays contents of a given file like 'more' utility in Linux
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Lex Program to print the total characters, white spaces, tabs in the given input file
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



