0% found this document useful (0 votes)
32 views2 pages

New Microsoft Office Word Document (4) QW

This C program uses pipes for interprocess communication between a parent and child process. The child process accepts a secret word from the user and sends it to the parent process through the pipe. The parent process generates an encoded code from parts of the word and returns it to the user after reversing and uppercase conversion.

Uploaded by

ketabrakbencong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

New Microsoft Office Word Document (4) QW

This C program uses pipes for interprocess communication between a parent and child process. The child process accepts a secret word from the user and sends it to the parent process through the pipe. The parent process generates an encoded code from parts of the word and returns it to the user after reversing and uppercase conversion.

Uploaded by

ketabrakbencong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

char result[100];
char* substring(char words[], int start, int n){
int i;
for (i=0; i<n; i++){
result[i] = words[start];
start++;
}
result[n] = '\0';
return result;
}

char* strrev (char words[]){
int i;
for (i=strlen(words)-1; i>= 0; i--){
result[strlen(words) - i - 1] = words[i];
}
result[strlen(words)] = '\0';
return result;
}

char* strtoupper(char words[]){
int i;
for (i=0;i <strlen(words); i++){
if(islower(words[i])){
words[i] = toupper(words[i]);
}
}
return words;
}


int main(){
pid_t pid;
int fd[2];
char words[100];
char dummy;

system("clear");
printf ("SIMPLE UNNAMED PIPE PROGRAM\n");
printf ("===========================\n");

if(pipe(fd) < 0){
perror("Pipe failed");
exit(EXIT_FAILURE);
}
pid = fork();
if(pid < 0){
perror("Fork failed");
exit(EXIT_FAILURE);
}
else if(pid == 0){
while(1){
close(fd[0]);
printf ("[CHILD]\n");
printf ("=======\n");
do{
printf ("Input your secret words [5..20] [\"exit\" to
close] : ");
scanf("%[^\n]s", words);
scanf("%c", &dummy);
if(strcasecmp(words, "exit")==0) break;
} while(strlen(words)<5 || strlen(words)>20);
write(fd[1], (char *) &words, sizeof(words));
if(strcasecmp(words, "exit")==0) break;
sleep(1);
}

}
else{
char code[100];
while(1){
close(fd[1]);
read(fd[0], (char *) &words, sizeof(words));

if(strcasecmp(words, "exit")==0) break;

printf ("\n[PARENT]\n");
printf ("=======\n");
printf ("Four first letters are %s\n", substring(words, 0,
4));
printf ("Four last letters are %s\n", substring(words,
strlen(words)-4, 4));
printf ("The length of the words is : %d\n",
strlen(words));
sprintf (code, "%s", substring(words, strlen(words)-4, 4));
sprintf (code, "%s%s%d", code, substring(words, 0, 4),
strlen(words));
printf ("So, the code is : %s\n", code);
printf ("Reverse and make it uppercase : %s\n\n",
strtoupper(strrev(code)));
}
}
return 0;
}

You might also like