Question & Answer: Write a program, changename.c, which takes a lename as input and produces a lename with a dierent extension as o…..

Write a program, changename.c, which takes a lename as input and produces a lename with a dierent extension as output. Most lenames consist of two parts, the basename (everything to the left of the last ‘.’ char- acter), and the extension (everything to the right of the last ‘.’ character). Often, we wish to name our output les something similar to our input les, and changing the extension is a convenient way to do this. For example, if the input le is called “myprog.in”, then an appropriate output le name would be “myprog.out”. Write your program to rst read a string (with no spaces in it) from the keyboard. This is the input lename (although we are not actually going to be reading from it). The program then creates an output string which is a a copy of the input string except that everything after the last ‘.’ character is replaced with the extension “out”. The input string and the output string are written to a le having the output lename. The output le should look like: input name is my.program.in output name is my.program.out

Expert Answer

 

/*Source code of the above problem is given below*/

#include<stdio.h>

#include <stdlib.h>

int main()

{

/*declaring necessary variables*/

char iFileName[500];

char oFileName[500];

char sentence[1000];

FILE *fptr;

char *temp;

int index;

sentence[0]=’’;

/*taking user input*/

printf(“Enter the input file name:n”);

gets(iFileName);

/*changing the extension*/

/*last occurance of ‘.’ is stored in index*/

temp = strrchr(iFileName, ‘.’);

index = (int)(temp – iFileName);

/*copying the input file name*/

strncpy(oFileName, iFileName, index+1);

oFileName[index+1] = ‘’;

/*concating the out extension*/

strcat(oFileName,”out”);

/*preparing the sentences to be written in the file*/

strcat(sentence,”input name is “);

strcat(sentence,iFileName);

strcat(sentence,”n”);

strcat(sentence,”output name is “);

strcat(sentence,oFileName);

//puts(sentence);

/*file having the output filename would be created in the same location where this program is located*/

fptr = fopen(oFileName, “w”);

if(fptr == NULL)

{

printf(“Error!”);

exit(1);

}

/*writing the sentences to file*/

fprintf(fptr,”%s”, sentence);

fclose(fptr);

return 0;

}

/*For better understanding code image ,output screen shot, and output file’s image is given below:*/

Question & Answer: Write a program, changename.c, which takes a lename as input and produces a lename with a dierent extension as o..... 1

Question & Answer: Write a program, changename.c, which takes a lename as input and produces a lename with a dierent extension as o..... 2

Question & Answer: Write a program, changename.c, which takes a lename as input and produces a lename with a dierent extension as o..... 3

Still stressed from student homework?
Get quality assistance from academic writers!