Skip to content
Related Articles

Related Articles

C Program for Lower Case to Uppercase and vice-versa in a file
  • Last Updated : 10 May, 2018

Lower Case To Upper Case

Given a text file(gfg.txt), our task is to convert all the lower case characters of file into upper case.

Examples:

Input: (content inside file (gfg.txt)
Geeks Classes:
An extensive classroom programme
by GeeksforGeeks to build and enhance
Data Structures and Algorithm concepts

Output: (content inside file (gfg.txt)
GEEKS CLASSES:
AN EXTENSIVE CLASSROOM PROGRAMME
BY GEEKSFORGEEKS TO BUILD AND ENHANCE
DATA STRUCTURES AND ALGORITHM CONCEPTS

Approach :
Open the file gfg.txt in read mode. Check if there is any error in opening or locating a file. If yes, then throw an error message.

If the file is found, then with the help of while loop, convert all the characters using toupper of that file into upper case.
Close the file using fclose() function by passing the file pointer in it.






// C++ program to convert
// all lower case characters of a file
// into Upper Case
#include <bits/stdc++.h>
  
int main()
{
    // initializing the file pointer
    FILE* fptr;
  
    // name of the file as sample.txt
    char file[50] = { "gfg.txt" };
    char ch;
  
    // opening the file in read mode
    fptr = fopen(file, "r");
    ch = fgetc(fptr);
  
    // converting into upper case
    while (ch != EOF) {
  
        // converting char to upper case
        ch = toupper(ch);
        printf("%c", ch);
        ch = fgetc(fptr);
    }
  
    // closing the file
    fclose(fptr);
  
    return 0;
}


Output:

GEEKS CLASSES:
AN EXTENSIVE CLASSROOM PROGRAMME
BY GEEKSFORGEEKS TO BUILD AND ENHANCE
DATA STRUCTURES AND ALGORITHM CONCEPTS

Upper Case to Lower Case:
Similar as above only using tolower function in place of toupper
Examples:

Input: (content inside file (gfg.txt)
Geeks Classes:
AN EXTENSIVE CLASSROOM PROGRAMME
BY GEEKSFORGEEKS TO BUILD AND ENHANCE
DATA STRUCTURES AND ALGORITHM CONCEPTS

Output: (content inside file (gfg.txt)
geeks classes:
an extensive classroom programme
by geeksforgeeks to build and enhance
data structures and algorithm concepts




// C++ program to convert all upper
// case characters of a file
// into lower Case
#include <bits/stdc++.h>
  
int main()
{
  
    // initializing the file pointer
    FILE* fptr;
  
    // name of the file as gfg.txt
    char file[30] = { "gfg.txt" };
    char ch;
  
    // opening the file in read mode
    fptr = fopen(file, "r");
    ch = fgetc(fptr);
  
    // converting into lower case
    while (ch != EOF) {
          
        // converting char to lower case
        ch = tolower(ch);
        printf("%c", ch);
        ch = fgetc(fptr);
    }
  
    // closing the file
    fclose(fptr);
      
    return 0;
}


Output:

geeks classes:
an extensive classroom programme
by geeksforgeeks to build and enhance
data structures and algorithm concepts

Note:

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.

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :