C program to convert uppercase to lowercase vice versa

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

This article explains how you can write a C program to convert uppercase to lowercase and vice versa in a file. It also explains logic to convert uppercase characters to lowercase and vice versa in C program.

Logic to convert uppercase to lowercase characters and vice versa in the file:

  • Open the source file in reading mode and get the file pointer in fptr. Also, check that file has been open successfully or not.
  • Create a temporary file.
  • Read a character from source file using c fgetc function.
  • Convert uppercase characters to lowercase and vice versa.
  • Write converted character to the temp file.
  • Close both files and delete the source file.
  • In the last, rename temporary file temp as the source file.

 

C code to convert uppercase to lowercase character in file

 

#include <stdio.h>
#include <ctype.h>

//Maximum size of the array
#define MAX_SIZE  32
#define FILE_NAME "aticleworld.txt"

//toggle file alphanumeric data
void toggleFileData(FILE *fptr)
{
    FILE *fpTmp = NULL;
    int ch = 0;

    //open the file
    fpTmp = fopen("tmp.txt", "w");
    if(fpTmp == NULL)
    {
        printf("Error in creating tmp file\n");
        fclose(fptr);
        exit(1);
    }

    //until EOF
    while ( (ch = fgetc(fptr)) != EOF)
    {
        /*
         * If current character is uppercase then toggle
         * it to lowercase and vice versa.
         */
        ch = (isupper(ch))? tolower(ch):toupper(ch);
        // write ch in temporary file.
        fputc(ch, fpTmp);
    }

    // Close all files to release resource
    fclose(fptr);
    fclose(fpTmp);


    // Delete original file
    remove(FILE_NAME);

    // Rename temporary file as original file
    rename("tmp.txt", FILE_NAME);
}

int main()
{
    //file pointer
    FILE *fp = NULL;
    //open the file
    fp = fopen(FILE_NAME, "r");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }

    /**
     * Function to convert lowercase characters to uppercase
     * and uppercase to lowercase in a file.
     * It also close the opened file
     */
    toggleFileData(fp);

    return 0;
}

File content before executing code,

C program to convert uppercase to lowercase

File content after executing code,

C program to convert uppercase to lowercase

 

Code Analysis:

In the above example, first, we opened the already created text (“aticleworld.txt”) file in reading mode and get the file pointer. Using the if condition I am verifying that file is opened successfully or not.

//open the file
   fp = fopen(FILE_NAME, "r");
   if(fp == NULL)
   {
       printf("Error in creating the file\n");
       exit(1);
   }

 

Called the “toggleFileData” function in which create a temporary file and get the file pointer.

//create the file
  fpTmp = fopen("tmp.txt", "w");
  if(fpTmp == NULL)
  {
      printf("Error in creating tmp file\n");
      fclose(fptr);
      exit(1);
  }

 

Read the file character using the c fgetc function and convert uppercase to lowercase or lowercase to uppercase. After toggling it write it into the created temporary file.

 

//until EOF
while ( (ch = fgetc(fptr)) != EOF)
{
    /*
     * If current character is uppercase then toggle
     * it to lowercase and vice versa.
     */
    ch = (isupper(ch))? tolower(ch):toupper(ch);
    // write ch in temporary file.
    fputc(ch, fpTmp);
}

 

Now close both files and remove the “aticleworld.txt” file.  In the last change the name of the temporary file with the original file name.

// Close all files to release resource
fclose(fptr);
fclose(fpTmp);
// Delete original file
remove(FILE_NAME);
// Rename temporary file as original file
rename("tmp.txt", FILE_NAME);

 

Recommended posts: