Upper Case to Lower Case
Given a text file(gfg.txt), our task is to convert all the uppercase characters of the file into lowercase.
Examples
Input: (content inside file (gfg.txt) AN EXTENSIVE CLASSROOM PROGRAMME BY w3wiki TO BUILD AND ENHANCE DATA STRUCTURES AND ALGORITHM CONCEPTS Output: (content inside file (gfg.txt) an extensive classroom programme by w3wiki to build and enhance data structures and algorithm concepts
Approach
- Open the file gfg.txt in write mode.
- Check if there is any error in opening or locating a file.
- If yes, then throw an error message.
- If no, write the contents of the reference string into the file and rewind the file pointer.
- Now, with the help of a while loop, convert all the characters of that file into the lower case using tolower() function and store them in a temporary buffer.
- Print the temporary buffer into the gfg.txt file.
- Close the file using fclose() function by passing the file pointer in it.
Program for Uppercase to Lowercase Conversion
C
// C program to convert all upper case characters of a file // into Lower Case #include <ctype.h> #include <stdio.h> #include <stdlib.h> // utility function to print file void readFile( FILE * fptr) { char ch = fgetc (fptr); while (ch != EOF) { printf ( "%c" , ch); ch = fgetc (fptr); } printf ( "\n" ); } int main() { // initializing the file pointer FILE * fptr; // test string char str[500] = "AN EXTENSIVE CLASSROOM PROGRAMME\nBY " "w3wiki TO BUILD AND ENHANCE\nDATA " "STRUCTURES AND ALGORITHM CONCEPTS" ; // name of the file as sample.txt char file[50] = { "gfg.txt" }; char ch; // opening the file in read write mode fptr = fopen (file, "w" ); if (fptr == NULL) { printf ( "Error Opening File" ); exit (0); } // printing data to the file fprintf (fptr, "%s" , str); rewind (fptr); // printing initial file printf ( "INITIAL FILE DATA:\n" ); readFile(fptr); rewind (fptr); ch = fgetc (fptr); int i = 0; // converting into lower case while (ch != EOF) { // converting char to upper case str[i++] = tolower (ch); ch = fgetc (fptr); } rewind (fptr); // printing upper case fprintf (fptr, "%s" , str); rewind (fptr); printf ( "\nFINAL FILE DATA:\n" ); readFile(fptr); // closing the file fclose (fptr); return 0; } |
Output
INITIAL FILE DATA: AN EXTENSIVE CLASSROOM PROGRAMME BY w3wiki TO BUILD AND ENHANCE DATA STRUCTURES AND ALGORITHM CONCEPTS FINAL FILE DATA: an extensive classroom programme by w3wiki to build and enhance data structures and algorithm concepts
Notes:
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 Program For Lower Case to Uppercase and Vice-Versa in a File
Given a text file(gfg.txt), our task is to convert all the lowercase characters of the file into uppercase.
Examples
Input: (content inside file gfg.txt) An extensive classroom programme by w3wiki to build and enhance Data Structures and Algorithm concepts Output: (content inside file gfg.txt) AN EXTENSIVE CLASSROOM PROGRAMME BY w3wiki TO BUILD AND ENHANCE DATA STRUCTURES AND ALGORITHM CONCEPTS
Approach
- Open the file gfg.txt in write mode.
- Check if there is any error in opening or locating a file.
- If yes, then throw an error message.
- If no, write the contents of the reference string into the file and rewind the file pointer.
- Now, with the help of a while loop, convert all the characters of that file into the upper case using toupper() function and store them in a temporary buffer.
- Print the temporary buffer into the gfg.txt file.
- Close the file using fclose() function by passing the file pointer in it.
Program for Lowercase to Uppercase Conversion
C
// C program to convert all lower case characters of a file // into Upper Case #include <ctype.h> #include <stdio.h> #include <stdlib.h> // utility function to print file void readFile( FILE * fptr) { char ch = fgetc (fptr); while (ch != EOF) { printf ( "%c" , ch); ch = fgetc (fptr); } printf ( "\n" ); } int main() { // initializing the file pointer FILE * fptr; // test string char str[500] = "An extensive classroom " "programme\nby w3wiki to build and " "enhance\nData Structures and Algorithm concepts" ; // name of the file as sample.txt char file[50] = { "gfg.txt" }; char ch; // opening the file in read write mode fptr = fopen (file, "w" ); // printing data to the file fprintf (fptr, "%s" , str); rewind (fptr); // printing initial file printf ( "INITIAL FILE DATA:\n" ); readFile(fptr); rewind (fptr); ch = fgetc (fptr); int i = 0; // converting into upper case while (ch != EOF) { // converting char to upper case str[i++] = toupper (ch); ch = fgetc (fptr); } rewind (fptr); // printing upper case fprintf (fptr, "%s" , str); rewind (fptr); printf ( "\nFINAL FILE DATA:\n" ); readFile(fptr); // closing the file fclose (fptr); return 0; } |
Output
INITIAL FILE DATA: An extensive classroom programme by w3wiki to build and enhance Data Structures and Algorithm concepts FINAL FILE DATA: AN EXTENSIVE CLASSROOM PROGRAMME BY w3wiki TO BUILD AND ENHANCE DATA STRUCTURES AND ALGORITHM CONCEPTS
Contact Us