C program to convert uppercase to lowercase and vice versa in file

Quick links

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.

  1. Open source file in read mode, store its reference in fptr.
  2. Create a temporary file to store result, store its reference in dest.
  3. Read a character from source file fptr. Store character read in ch
  4. Convert uppercase characters to lowercase and vice versa. Which means if isupper(ch) then convert to lowercase otherwise convert to uppercase.
  5. Write converted character to dest file.
  6. Repeat step 3-5 till end of file.
  7. Close both files fptr as well as dest.
  8. Remove source file fptr.
  9. 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 😉