C Program For Lower Case to Uppercase and Vice-Versa in a File
Last Updated :
26 Apr, 2023
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 GeeksforGeeks to build and enhance
Data Structures and Algorithm concepts
Output: (content inside file gfg.txt)
AN EXTENSIVE CLASSROOM PROGRAMME
BY GEEKSFORGEEKS 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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
void readFile( FILE * fptr)
{
char ch = fgetc (fptr);
while (ch != EOF) {
printf ( "%c" , ch);
ch = fgetc (fptr);
}
printf ( "\n" );
}
int main()
{
FILE * fptr;
char str[500]
= "An extensive classroom "
"programme\nby GeeksforGeeks to build and "
"enhance\nData Structures and Algorithm concepts" ;
char file[50] = { "gfg.txt" };
char ch;
fptr = fopen (file, "w" );
fprintf (fptr, "%s" , str);
rewind (fptr);
printf ( "INITIAL FILE DATA:\n" );
readFile(fptr);
rewind (fptr);
ch = fgetc (fptr);
int i = 0;
while (ch != EOF) {
str[i++] = toupper (ch);
ch = fgetc (fptr);
}
rewind (fptr);
fprintf (fptr, "%s" , str);
rewind (fptr);
printf ( "\nFINAL FILE DATA:\n" );
readFile(fptr);
fclose (fptr);
return 0;
}
|
Output
INITIAL FILE DATA:
An extensive classroom programme
by GeeksforGeeks to build and enhance
Data Structures and Algorithm concepts
FINAL FILE DATA:
AN EXTENSIVE CLASSROOM PROGRAMME
BY GEEKSFORGEEKS TO BUILD AND ENHANCE
DATA STRUCTURES AND ALGORITHM CONCEPTS
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 GEEKSFORGEEKS TO BUILD AND ENHANCE
DATA STRUCTURES AND ALGORITHM CONCEPTS
Output: (content inside file (gfg.txt)
an extensive classroom programme
by geeksforgeeks 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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
void readFile( FILE * fptr)
{
char ch = fgetc (fptr);
while (ch != EOF) {
printf ( "%c" , ch);
ch = fgetc (fptr);
}
printf ( "\n" );
}
int main()
{
FILE * fptr;
char str[500]
= "AN EXTENSIVE CLASSROOM PROGRAMME\nBY "
"GEEKSFORGEEKS TO BUILD AND ENHANCE\nDATA "
"STRUCTURES AND ALGORITHM CONCEPTS" ;
char file[50] = { "gfg.txt" };
char ch;
fptr = fopen (file, "w" );
if (fptr == NULL) {
printf ( "Error Opening File" );
exit (0);
}
fprintf (fptr, "%s" , str);
rewind (fptr);
printf ( "INITIAL FILE DATA:\n" );
readFile(fptr);
rewind (fptr);
ch = fgetc (fptr);
int i = 0;
while (ch != EOF) {
str[i++] = tolower (ch);
ch = fgetc (fptr);
}
rewind (fptr);
fprintf (fptr, "%s" , str);
rewind (fptr);
printf ( "\nFINAL FILE DATA:\n" );
readFile(fptr);
fclose (fptr);
return 0;
}
|
Output
INITIAL FILE DATA:
AN EXTENSIVE CLASSROOM PROGRAMME
BY GEEKSFORGEEKS TO BUILD AND ENHANCE
DATA STRUCTURES AND ALGORITHM CONCEPTS
FINAL FILE DATA:
an extensive classroom programme
by geeksforgeeks 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.
Share your thoughts in the comments
Please Login to comment...