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

Using program in the C language. Write a function, changename, which takes a fil

ID: 3768170 • Letter: U

Question

Using program in the C language.

Write a function, changename, which takes a filename as input and produces a filename with a different extension as output in a second string. That is, the input filename string is copied to the output filename string, except that the extension on the latter is changed. The desired new extension should be provided as a third string to the function.

Most filenames 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 files something similar to our input files, and changing the extension is a convenient way to do this. For example, if the input file is called "myprog.in", then an appropriate output file name would be "myprog.out".

Write a main program to first read a string (with no spaces in it) from the keyboard. This is the ‘input’ filename (although we will not actually read from this file). The main function then calls changename to create the ‘output’ string which, as explained above, is 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 file having the modified filename.

For example, if the filename "my.program.in" is entered, the output file my.program.out should contain (and you should test your program using an input filename having more than one ‘.’ character in it, as shown here):

Explanation / Answer

Here is the code for you. If you need any clarifications, just get back to me.

#include <stdio.h>
#include <string.h>
void changeName(char input[], char output[])
{
int i;  
strcpy(output, input);           //Copies the input string also to output string.
for(i = strlen(output)-1; i >= 0; i--)   //Finds the . position in the output string.
if(output[i] == '.')                       //If the index is found, come out of the loop.
break;
strcpy(output+i, ".out");                 //Write .out from that position, in the output string.
}
int main()
{
char input[50], output[50];
printf("Enter the name of the input file: ");
scanf("%s", input);   //Reads the input file name.
changeName(input, output);   //Changes the input string to output string.
printf("Input file name is: %s ", input);   //Prints the input string.
printf("Output file name is: %s ", output);   //Prints the output string.
FILE *fp = fopen(output, "w");               //Opens the output string as a file.
fprintf(fp, "Input file name is: %s ", input);   //Writes the input string to file.
fprintf(fp, "Output file name is: %s ", output);   //Writes the output string to file.
fclose(fp);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote