This blog is under construction

Monday 29 July 2013

C program to convert lowercase characters in a file to uppercase

Write a C program to convert the file contents in lowercase to uppercase.


  #include <stdio.h>
  #include <string.h>
  #define MAX 256

  int main() {
        int ch;
        FILE *fp1, *fp2;
        char src[MAX], dest[MAX];

        /* get the source file name from the user */
        printf("Enter your source file name:");
        fgets(src, MAX, stdin);
        src[strlen(src) - 1] = '\0';

        /* get the destination file name from the user */
        printf("Enter your destination file name:");
        fgets(dest, MAX, stdin);
        dest[strlen(dest) - 1] = '\0';

        /* open the source file in read mode */
        fp1 = fopen(src, "r");

        /* error handling */
        if (!fp1) {
                printf("Unable to open the file %s\n", src);
                return 0;
        }

        /* open the destination file in write mode */
        fp2 = fopen(dest, "w");

        /* error handling */
        if (!fp2) {
                printf("Unable to open the file %s\n", dest);
                fclose(fp1);
                return 0;
        }

        /* coverts lowercase to uppercase */
        while (!feof(fp1)) {
                ch = fgetc(fp1);

                if (ch == EOF) {
                        continue;
                } else if (ch >= 'a' && ch <= 'z') {
                        fputc((ch - 'a' + 'A'), fp2);
                } else {
                        fputc(ch, fp2);
                }
        }

        /* close the opened files */
        fclose(fp1);
        fclose(fp2);

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your source file name: data.txt
  Enter your destination file name: output.txt

  jp@jp-VirtualBox:~/$ cat data.txt 
  Take up one idea. Make that one idea your life -
  think of it, dream of it, live on that idea. Let 
  the brain, muscles, nerves, every part of your 
  body, be full of that idea, and just leave every 
  other idea alone. This is the way to success.

  jp@jp-VirtualBox:~/$ cat output.txt 
  TAKE UP ONE IDEA. MAKE THAT ONE IDEA YOUR LIFE -
  THINK OF IT, DREAM OF IT, LIVE ON THAT IDEA. LET 
  THE BRAIN, MUSCLES, NERVES, EVERY PART OF YOUR 
  BODY, BE FULL OF THAT IDEA, AND JUST LEAVE EVERY 
  OTHER IDEA ALONE. THIS IS THE WAY TO SUCCESS.






SEE ALSO

    No comments:

    Post a Comment