C Program to Convert the Content of File to UpperCase

This C Program converts the content of file to UpperCase.

Here is source code of the C Program to convert the content of file to UpperCase. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Convert the Content of File to UpperCase
  3.  */
  4. #include <stdio.h>
  5.  
  6. int to_upper_file(FILE *);
  7.  
  8. int main(int argc,char *argv[])
  9. {
  10.     FILE *fp;
  11.     int status;
  12.  
  13.     if (argc == 1)
  14.     {
  15.         printf("Insufficient Arguments:");
  16.         printf("No File name is provided at command line");
  17.         return;
  18.     }
  19.     if (argc > 1)
  20.     {
  21.         fp = fopen(argv[1],"r+");
  22.         status = to_upper_file(fp);
  23.  
  24.         /* 
  25.           *If the status returned is 0 then the coversion of file content was completed successfully
  26.          */
  27.  
  28.         if (status == 0)
  29.         {
  30.             printf("\n The content of \"%s\" file was successfully converted to upper case\n",argv[1]);
  31.             return;
  32.         }
  33.         /*
  34.           * If the status returnes is -1 then the conversion of file content was not done
  35.           */
  36.         if (status == -1)
  37.         {
  38.             printf("\n Failed to convert");
  39.             return;
  40.         } 
  41.    }
  42. }
  43.  
  44. /*
  45.  * convert the file content to uppercase
  46.  */
  47. int to_upper_file(FILE *fp)
  48. {
  49.     char ch;
  50.  
  51.     if (fp == NULL)
  52.     {
  53.         perror("Unable to open file");
  54.         return -1;
  55.     }
  56.     else
  57.     {
  58.         /*
  59.           * Read the file content and convert to uppercase
  60.           */
  61.         while (ch != EOF)
  62.         {
  63.             ch = fgetc(fp);
  64.             if ((ch >= 'a') && (ch <= 'z'))
  65.             {
  66.                 ch = ch - 32;
  67.                 fseek(fp,-1,SEEK_CUR);
  68.                 fputc(ch,fp);
  69.             }    
  70.         }
  71.         return 0;
  72.     }
  73. }

/* Input file : mydata
$ cat mydata
This is Manish
I had worked in Wipro and Cisco
*/
 
$ gcc file3.c
$ a.out mydata
The content of "mydata" file was successfully converted to upper case
 
/* "mydata" after conversion
$ cat mydata
THIS IS MANISH
I HAD WORKED IN WIPRO AND CISCO
*/

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.