示例#1
0
void  invert(char* s) {
  int i;
  char temp;
  int sl = strlen(s);
  for (i=0; i<sl/2; i++) {
    temp = s[i];
    s[i] = s[sl-i-1];
    s[sl-i-1] = temp;
  }
  swapCase(s);
}
示例#2
0
文件: pipes.c 项目: couchjd/cs433
void encoder(char* argv){
  int fd1[2];
  int fd2[2];
  int nbytes;
  int counter;
  char readbuffer[256];
  char* string; 

  if(argv == NULL){
    string = getString();
  }
  else{
    string = argv;
  }

  pid_t childpid;

  pipe(fd1);
  pipe(fd2);

  if((childpid = fork()) == -1){
    perror("fork");
    exit(1);
  }

  //child process
  if(childpid == 0){
    close(fd1[STDOUT]); //close extra child output pipe
    close(fd2[STDIN]);  //close extra child input pipe
    write(fd2[STDOUT], string, (strlen(string)+1)); //send message to parent process
    read(fd1[STDIN], readbuffer, sizeof(readbuffer)); //receive encoded message from parent
    printf("Encoded message: \t%s\n", readbuffer);
    exit(0);
  }

  //parent process
  else{
    close(fd2[STDOUT]); //close parent output pipe
    close(fd1[STDIN]);  //close parent input pipe
    nbytes = read(fd2[STDIN], readbuffer, sizeof(readbuffer)); //read original message from child 
    printf("Original message: \t%s", readbuffer);
    
    //encode message
    for(counter = 0; counter < nbytes - 1; counter++){
      readbuffer[counter] = swapCase(encode(readbuffer[counter]));
    }
    
    write(fd1[STDOUT], readbuffer, (strlen(readbuffer)+1)); //send message back to child
  }
}