Write a C program to convert uppercase to lowercase and vice versa in file. How to convert uppercase characters to lowercase and vice versa in a file in C programming. Logic to convert uppercase characters to lowercase and vice versa in C program.
Required knowledge
Basic Input Output, String, File handling
Logic to convert uppercase to lowercase characters and vice versa in file
Step by step descriptive logic to convert uppercase characters to lowercase and vice versa.
- Open source file in read mode, store its reference in
fptr
. - Create a temporary file to store result, store its reference in
dest
. - Read a character from source file
fptr
. Store character read inch
- Convert uppercase characters to lowercase and vice versa. Which means if
isupper(ch)
then convert to lowercase otherwise convert to uppercase. - Write converted character to
dest
file. - Repeat step 3-5 till end of file.
- Close both files
fptr
as well asdest
. - Remove source file
fptr
. - Rename temporary file
dest
as path of source file.
Program to convert uppercase to lowercase character in file
/**
* C program to convert lowercase to uppercase and uppercase to lowercase
* characters in file.
*/
#include <stdio.h>
#include <stdlib.h>
void toggleCase(FILE *fptr, const char *path);
int main()
{
/* File pointer to hold reference of input file */
FILE *fPtr;
char path[100];
printf("Enter path of source file: ");
scanf("%s", path);
fPtr = fopen(path, "r");
/* fopen() return NULL if unable to open file in given mode. */
if (fPtr == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open file.\n");
printf("Please check whether file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
toggleCase(fPtr, path);
printf("\nSuccessfully converted characters in file from uppercase to lowercase and vice versa.\n");
return 0;
}
/**
* Function to convert lowercase characters to uppercase
* and uppercase to lowercase in a file.
*/
void toggleCase(FILE *fptr, const char *path)
{
FILE *dest;
char ch;
// Temporary file to store result
dest = fopen("toggle.tmp", "w");
// If unable to create temporary file
if (dest == NULL)
{
printf("Unable to toggle case.");
fclose(fptr);
exit(EXIT_FAILURE);
}
/* Repeat till end of file. */
while ( (ch = fgetc(fptr)) != EOF)
{
/*
* If current character is uppercase then toggle
* it to lowercase and vice versa.
*/
if (isupper(ch))
ch = tolower(ch);
else if (islower(ch))
ch = toupper(ch);
// Print toggled character to destination file.
fputc(ch, dest);
}
/* Close all files to release resource */
fclose(fptr);
fclose(dest);
/* Delete original source file */
remove(path);
/* Rename temporary file as original file */
rename("toggle.tmp", path);
}
Suppose <strong>data/file3.txt</strong>
before executing program contains.
I love programming.
---------PROGRAMMING IN C---------
Learning C programming at Codeforwin is simple and easy.
After executing program <strong>data/file3.txt</strong>
contains.
i LOVE PROGRAMMING.
---------programming in c---------
lEARNING c PROGRAMMING AT cODEFORWIN IS SIMPLE AND EASY.
Output
Enter path of source file: data/file3.txt Successfully converted characters in file from uppercase to lowercase and vice versa.
Happy coding 😉