C Program for Lower Case to Uppercase and vice-versa in a file

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.