Write a C/C++ program (call it string_invert) that takes a string argument from
ID: 3608719 • Letter: W
Question
Write a C/C++ program (call it string_invert) that takes astring argument from the command line and outputs the string inreversed order. Here comes
the twist: Each process can output at most one character. If youwant to output more than a
single character, you must fork off one or more processes in orderto do that, and each of the
forked processes in turn outputs a single character. After the callto program string_invert
with the command line argument, the output should appear, and nomore processes should
be running, in addition to the shell. Test your program on anyUNIX/LINUX machine, and
turn in the source code as part of the written assignment. (Thesource code should be at
most a few lines long.)
Explanation / Answer
//Here is the C program, compiled ingcc #include #include #include #include #define MAX_RF_BUFFER 100 int main(int argc,char *argv[]){ char s[MAX_RF_BUFFER]; int i,ts=0; char *ptr; pid_t pid; if(argc==1) { printf("Enter the string:"); scanf("%s",s); } else strcpy(s,(char*)argv[1]); printf("Reverse is:"); fflush(stdout); ptr=s; for(i=strlen(s)-1;i>=0;i--) { if(pid=fork()==0) { printf("%c",ptr[i]); break; } wait(&ts); } if(pid==0) printf(" "); } /* Sample output Enter the string:hello Reverse is:olleh */Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.