please use linux/unix command terminal to complete,
step 1
1-create a new directory named by inlab4
enter directory inlab4
create a new file named by reverse.c with the following contents and then close the file:
/*reverse.c */
#include <stdio.h>
reverse(char *before, char *after);
main()
{
char str[100]; /*Buffer to hold reversed string */
reverse("cat", str); /*Reverse the string "cat" */
printf("reverse(\"cat\")=%s\n", str); /*Display result */
reverse("noon", str); /*Reverse the string "noon" */
printf("reverse(\"noon\")=%s\n", str); /*Display result */
}
reverse(char *before, char *after)
{
int i;
int j;
int len;
len=strlen(before);
for(j=len-1, i=0; j>=0; j--, i++)
after[i]=before[j];
after[len]=NULL;
}
step 2-create a new file named by reverse.h with the following contents:
/* reverse.h */
reverse(char *before, char *after); /* declare but do not define this function */
step 3modify your file of reverse.c to the following contents:
/*reverse.c */
#include <stdio.h>
#include "reverse.h"
reverse(char *before, char *after)
{
int i;
int j;
int len;
len=strlen(before);
for(j=len-1, i=0; j>=0; j--, i++)
after[i]=before[j];
after[len]=NULL;
}
step 4 a new file named by main1.c with the following contents:
/*main.c */
#include <stdio.h>
#include "reverse.h"
main()
{
char str[100]; /*Buffer to hold reversed string */
reverse("cat", str); /*Reverse the string "cat" */
printf("reverse(\"cat\")=%s\n", str); /*Display result */
reverse("noon", str); /*Reverse the string "noon" */
printf("reverse(\"noon\")=%s\n", str); /*Display result */
}
step 5
List all the directory and files under the current path, and you will find a directory named by inlab4.
copy all the contents of inlab4 to inlab6 (create the directory if you don’t have it), you can use the following command:
cp –r inlab4 inlab6
create a new file named by palindrome.h
/* PALINDROME.H */
int palindrome(); /* Declare but do not define */
step 6 -create a new file named by palindrome.c:
/* palindrome.c */
#include "palindrome.h"
#include "reverse.h"
#include <string.h>
/*****************************************/
int palindrome(char *str)
{
char reversedStr[100];
reverse(str, reversedStr); /*Reverse original */
return (strcmp(str, reversedStr)==0); /* compare the two */
}
step 7 create a new file named by main2.c:
/* main2.c */
#include <stdio.h>
#include "palindrome.h"
/*****************************************************/
main()
{
printf("palindrome (\"cat\") = %d\n", palindrome("cat"));
printf("palindrome (\"noon\") = %d\n", palindrome("noon"));
}
step 8-instruction: compile palindrome.c and main2.c, link all files needed and run main2
step 9 -Now if you modify a little bit for reverse.c, then you have to recompile reverse.c, recompile palindrome.c, recompile main2.c, relink three objective files and then rerun it. That is, you have to retype four commands. Now we create a makefile and then put these commands in one file, and you can use one command to run these four commands.
create a new file named main1.make with the following contents:
main1: main1.o reverse.o
cc main1.o reverse.o -o main1
main1.o: main1.c reverse.h
cc -c main1.c
reverse.o: reverse.c reverse.h
cc -c reverse.c
step 10 create a new file named main2.make with the following contents:
main2:
main2.o reverse.o palindrome.o
cc main2.o reverse.o palindrome.o -o main2
main2.o:
main2.c palindrome.h
cc -c main2.c
reverse.o:
reverse.c reverse.h
cc -c reverse.c
palindrome.o:
palindrome.c palindrome.h reverse.h
cc -c palindrome.c
step 11 - type the following command and print the results
make –f main1.make
step 12-type the following command and print the results:
make –f main2.make
// can you provide the commands necessary to
//compile palindrome.c and main2.c, link all needed files and then run main2.
Step 1: To create a new directory named by inlab4
Open the terminal and type the below command:
mkdir inlab4
To enter directory inlab4
cd inlab4
To create a new file named by reverse.c
gedit reverse.c
Save and close the reverse.c file with the following contents:
/*reverse.c */
#include <stdio.h>
reverse(char *before, char *after);
main()
{
char str[100]; /*Buffer to hold reversed string */
reverse("cat", str); /*Reverse the string "cat" */
printf("reverse(\"cat\")=%s\n", str); /*Display result */
reverse("noon", str); /*Reverse the string "noon" */
printf("reverse(\"noon\")=%s\n", str); /*Display result */
}
reverse(char *before, char *after)
{
int i;
int j;
int len;
len=strlen(before);
for(j=len-1, i=0; j>=0; j--, i++)
after[i]=before[j];
after[len]=NULL;
}
Step 2: To create a new file named by reverse.h
gedit reverse.h
Save and close the reverse.h file with the following contents:
/* reverse.h */
reverse(char *before, char *after); /* declare but do not define this function */
Step 3: To modify the file reverse.c
gedit reverse.c
Save and close the reverse.c file with the following contents:
/*reverse.c */
#include <stdio.h>
#include "reverse.h"
reverse(char *before, char *after)
{
int i;
int j;
int len;
len=strlen(before);
for(j=len-1, i=0; j>=0; j--, i++)
after[i]=before[j];
after[len]=NULL;
}
Step 4: To create a new file named by main1.c
gedit main1.c
Save and close the main1.c file with the following contents:
/*main1.c */
#include <stdio.h>
#include "reverse.h"
main()
{
char str[100]; /*Buffer to hold reversed string */
reverse("cat", str); /*Reverse the string "cat" */
printf("reverse(\"cat\")=%s\n", str); /*Display result */
reverse("noon", str); /*Reverse the string "noon" */
printf("reverse(\"noon\")=%s\n", str); /*Display result */
}
Step 5: To list all the directory and files under the current path, and you will find a directory named by inlab4.
Type the below command to go to parent directory of inlab4:
cd ..
Command to List all the directory and files under the current path
ls
now the directory named inlab4 is listed.
To copy all the contents of inlab4 to inlab6, create a directory named inlab6 and copy
mkdir inlab6
cp inlab4/*
inlab6
Step 6: To create a new file named by palindrome.h in the directory inlab6
cd inlab6
gedit palindrome.h
Save and close the palindrome.h file with the following contents:
/* PALINDROME.H */
int palindrome(); /* Declare but do not define */
To create a new file named by palindrome.c
gedit palindrome.c
save and close the palindrome.c file with the following contents:
/* palindrome.c */
#include "palindrome.h"
#include "reverse.h"
#include <string.h>
/*****************************************/
int palindrome(char *str)
{
char reversedStr[100];
reverse(str, reversedStr); /*Reverse original */
return (strcmp(str, reversedStr)==0); /* compare the two */
}
Step 7: To create a new file named by main2.c
gedit main2.c
Save and close the main2.c file with the following contents:
/* main2.c */
#include <stdio.h>
#include "palindrome.h"
/*****************************************************/
main()
{
printf("palindrome (\"cat\") = %d\n", palindrome("cat"));
printf("palindrome (\"noon\") = %d\n", palindrome("noon"));
}
Step 8: To compile palindrome.c and main2.c, link all files needed and run main2, type the below commands:
cc palindrome.c reverse.c main2.c -o main2
./main2
Step 9: To create a new file named by main1.make in the directory inlab4
cd ../inlab4
gedit main1.make
Save and close the main1.make file with the following contents:
main1: main1.o reverse.o
cc main1.o reverse.o -o main1
main1.o: main1.c reverse.h
cc -c main1.c
reverse.o: reverse.c reverse.h
cc -c reverse.c
Step 10: To create a new file named by main2.make in the directory inlab6
cd ../inlab6
gedit main2.make
Save and close the main2.make file with the following contents:
main2: main2.o reverse.o palindrome.o
cc main2.o reverse.o palindrome.o -o main2
main2.o: main2.c palindrome.h
cc -c main2.c
reverse.o: reverse.c reverse.h
cc -c reverse.c
palindrome.o: palindrome.c palindrome.h reverse.h
cc -c palindrome.c
Step 11: To type the following command and print the results
make –f main1.make
Output of the above command:
cc main1.o reverse.o -o main1
Output of the program on executing the file: ./main1
reverse("cat")=tac
reverse("noon")=noon
Step 12: To type the following command and print the results:
make –f main2.make
Output of the above command:
cc main2.o reverse.o palindrome.o -o main2
Output of the program on executing the file: ./main2
palindrome ("cat") = 0
palindrome ("noon") = 1
Get Answers For Free
Most questions answered within 1 hours.