Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a C program that reads in the characters from the file “inputfile.txt” a

ID: 670996 • Letter: D

Question

Develop a C program that reads in the characters from the file “inputfile.txt” and writes the characters to the file “outputfile.txt”, with one modification; change all uppercase letters to lowercase letters, and change all lowercase letters to uppercase letters. Do not modify non-alphabetic characters. For example, if aBCd! is read in from “inputfile.txt” then AbcD! must be printed out to the file “outputfile.txt” Write three functions to accomplish this (these three functions must be called from within the main routine to receive full credit): // IsUpper passes back 1 if c is uppercase, // 0 if c is lower case, // and -1 if c is non-alphabetic int IsUpper(char c); char Upper2Lower(char c);// This function changes uppercase letters to lowercase char Lower2Upper(char c);// This function changes lowercase letters to uppercase Helpful Hint (but not necessary): Write the code using a switch statement, e.g. switch( IsUpper(c) ){ }

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
int IsUpper(char c);
char Upper2Lower(char c);
char Lower2Upper(char c);
int main(){
   FILE *fp1,*fp2;
   char ch;
   fp1=fopen("inputfile.txt","r");
   if(fp1==NULL){
       printf("Inputfile Doesnot Exist");
       exit(1);
   }
   fp2=fopen("outputfile.txt","w");
   if(fp2==NULL){
       printf("Output File Creation Problem");
       exit(1);
   }
   while((ch=fgetc(fp1))!=EOF){
       printf("%c",ch);
       switch(IsUpper(ch)){
           case 0:
                   ch=Lower2Upper(ch);
                   break;
           case 1:
           ch=Upper2Lower(ch);
           break;
           case -1:
           break;
           default:
           break;
       }
       fputc(ch,fp2);
   }
   fclose(fp1);
   fclose(fp2);
  
   fp2=fopen("outputfile.txt","r");
   while((ch=getc(fp2))!=EOF){
       printf("%c",ch);
   }
   fclose(fp2);
}
int IsUpper(char c){
   if(c>='A' && c<='Z')
   return 1;
   else if (c>='a' && c<='z')
   return 0;
   else
   return -1;
}
char Upper2Lower(char c){
   return c+32; //ASCII VALUES A-65 a-97 Difference: 32
}
char Lower2Upper(char c){
   return c-32;
}